|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
第四第4.7.10下面一个例子,汇总同文件夹下多工作簿数据。
书中提供的代码如下:
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
------------------------------------------
我自己根据练习的时候写了代码如下,但就是在wb处运行不了,说方法和数据成员未找到。
Public Sub 试试()
Application.ScreenUpdating = False
Dim r As Integer, c As Integer, filename As String, erow As Integer, fn As String, wb As Worksheet, sht As Worksheet, arr
r = 1
c = 8
Worksheets("sheet1").Range(Cells(r + 1, "A"), Cells(65536, c)).ClearContents
Let filename = Dir(thisbook.Path & "\*.xls")
Do While filename <> ""
If filename <> ThisWorkbook.Name Then
Let erow = Range("A1").CurrentRegion.Rows.Count + 1
Let fn = ThisWorkbook.Path & "\" & filename
Set wb = GetObject(fn)
Set sht = wb.Worksheets.Item(1)
Let arr = sht.Range(sht.Cells(r, "A"), sht.Cells(65536, "A").End(xlUp).Offset(0, 8))
Cells(erow, A).Resize(UBound(arr, 1), UBound(arr, 2)) = arr
wb.Close False
End If
filename = Dir
Loop
Application.ScreenUpdating = True
End Sub
-------------------------------------
请教大神是哪出错了,谢谢
|
|