返回列表 上一主題 發帖

[發問] 如資料行、列數不一定如何統一合併為兩攔且以空格分開

呈上 因資料筆數較多 請各位前輩賜教 感恩
billchenfantasy 發表於 2013-2-18 14:16


觀看你舉例的結果與上方原稿內容似乎有部分地方無法對應,例如 :
M12 雖然有對應 2002/9/24
但找不到 M63 (0-0 2-2) 裡的 2-2 (M64 是 1974/8/22 2002/9/24)
故假設你原稿中 Plan_Dat_5 與 Plan_Dat_6 (即 L 欄與 M 欄間)中間有一欄被誤刪了.
底下的程式是以此假設為基礎做出來的.
  1. Sub ex()
  2.   Dim iCols%, iCol%
  3.   Dim lRows As Long, lRow As Long
  4.   
  5.   iCols = Cells(1, Columns.Count).End(xlToLeft).Column
  6.   Range(Columns(iCols - 1), Columns(iCols)).Cut
  7.   Columns(3).Insert xlShiftToRight
  8.   lRows = Cells(Rows.Count, 1).End(xlUp).Row
  9.   
  10.   For lRow = 2 To lRows
  11.     For iCol = 3 To iCols Step 2
  12.       If Cells(lRow, iCol) <> "" Then
  13.         Cells(lRow, 1) = Cells(lRow, 1) & " " & Cells(lRow, iCol)
  14.         Cells(lRow, iCol) = ""
  15.         Cells(lRow, 2) = Cells(lRow, 2) & " " & Cells(lRow, iCol + 1)
  16.         Cells(lRow, iCol + 1) = ""
  17.       End If
  18.     Next iCol
  19.   Next lRow
  20.   End Sub
複製代碼

TOP

        靜思自在 : 人生沒有所有權,只有生命的使用權。
返回列表 上一主題