下面的代碼可以做到這一點,因爲這一切都發生在activesheet上。
Sub consolidate()
Dim lastRow As Integer
Dim lastCol As Integer
Dim counterRow As Integer
Dim counterCol As Integer
Dim counterDestinationRow As Integer
Dim destination As Object
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Set destination = Cells(lastRow + 2, 1)
'or to another sheet: Set destination = Sheets("Sheet2").Cells(1, 1)
For counterRow = 1 To lastRow
lastCol = Cells(counterRow, Columns.Count).End(xlToLeft).Column
For counterCol = 2 To lastCol
destination.Offset(counterDestinationRow, 0) = Cells(counterRow, 1)
destination.Offset(counterDestinationRow, 1) = Cells(counterRow, counterCol)
counterDestinationRow = counterDestinationRow + 1
Next counterCol
Next counterRow
End Sub
Marco建議的宏工作!非常感謝所有 – Adi