本文为《别怕,Excel VBA其实特别简单(第3版)》随书问题参考答案
要解决本例中的问题,可以将过程改写为:
Sub 合并多表数据()
Dim EndRow As Long, DataArr As Variant, ToRng As Range
Dim ToSht As Worksheet, Sht As Worksheet
Set ToSht = Worksheets("汇总结果") '变量ToSht是保存汇总结果的工作表
ToSht.Rows("2:1048576").Clear '清除表中原有数据
Dim EndCol As Long '保存合并区域的最后一列列号
For Each Sht In Worksheets
If Sht.Name ToSht.Name Then '排除保存汇总结果的工作表
EndRow = Sht.Range("A1048576").End(xlUp).Row
EndCol = Sht.UsedRange.Columns.Count '最后一列列号
DataArr = Sht.Range(Sht.Cells(2, "A"), Sht.Cells(EndRow, EndRow)).Value
Set ToRng = ToSht.Range("A1048576").End(xlUp).Offset(1, 0)
ToRng.Resize(UBound(DataArr, 1), EndCol) = DataArr
End If
Next Sht
End Sub