下面的代码可以做到这一点,因为这一切都发生在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