|
如下面代码中,标红部分:
Sub HzWb()
Dim bt As Range, r As Long, c As Long
r = 1 '1 是表头的行数
c = 8 '8 是表头的列数
Range(Cells(r + 1, "A"), Cells(65536, c)).ClearContents ' 清除汇总表中原表数据
Application.ScreenUpdating = False
Dim FileName As String, wb As Workbook, Erow As Long, fn As String, arr As Variant
FileName = Dir(ThisWorkbook.Path & "\*.xls")
Do While FileName <> ""
If FileName <> ThisWorkbook.Name Then ' 判断文件是否是本工作簿
Erow = Range("A1").CurrentRegion.Rows.Count + 1 ' 取得汇总表中第一条空行行号
fn = ThisWorkbook.Path & "\" & FileName
Set wb = GetObject(fn) ' 将fn 代表的工作簿对象赋给变量
Set sht = wb.Worksheets(1) ' 汇总的是第1 张工作表
' 将数据表中的记录保存在arr 数组里
arr = sht.Range(sht.Cells(r + 1, "A"), sht.Cells(65536, "B").End(xlUp).Offset(0, 8))
' 将数组arr 中的数据写入工作表
Cells(Erow, "A").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
wb.Close False
End If
FileName = Dir ' 用Dir 函数取得其他文件名,并赋给变量
Loop
Application.ScreenUpdating = True
End Sub |
|