如何利用VBA引用多个Excel文件且不同sheet之间实现数据汇总
1个回答
展开全部
试试下面的代码。把文件夹中的所有表格遍历
Sub test()
Dim r%, i%
Dim arr, brr(1 To 1000, 1 To 11)
Dim mypath$, myname$
Dim wb As Workbook
Dim ws As Worksheet
Application.ScreenUpdating = fasle
Application.DisplayAlerts = fasle
mypath = ThisWorkbook.Path & ""
myname = Dir(mypath & "*.xls")
m = 0
Do While myname <> ""
If myname <> "汇总表.xls" Then
Set wb = GetObject(mypath & myname)
With wb
With .Worksheets("sheet1")
arr = .Range("a1:j32")
For Each x In Array(2, 9, 15, 21, 27)
If Len(arr(x, 2)) <> 0 Then
m = m + 1
brr(m, 1) = arr(x, 6)
brr(m, 2) = arr(x, 2)
brr(m, 3) = arr(x, 4)
brr(m, 4) = IIf(x = 2, arr(x, 8), arr(x, 10))
brr(m, 5) = arr(x + 1, 7)
brr(m, 6) = arr(x + 2, 2)
brr(m, 7) = arr(x + 3, 2)
brr(m, 8) = arr(x + 3, 8)
brr(m, 9) = arr(x + 4, 2)
brr(m, 10) = IIf(x = 2, "户主", arr(x, 8))
brr(m, 11) = arr(x + 5, 2)
End If
Next
End With
.Close False
End With
End If
myname = Dir()
Loop
With Worksheets("sheet1")
.UsedRange.Offset(2, 0).ClearContents
.Range("a3").Resize(UBound(brr), UBound(brr, 2)) = brr
End With
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询