|
还是用VBA比较好
Sub 合并数据()
Dim wk As Workbook, wsh As Worksheet, i As Long, brr()
a = Dir("C:\Users\Administrator\Desktop\新建文件夹 (3)\*.xlsm")
Workbooks.Open ("C:\Users\Administrator\Desktop\新建文件夹 (3)\" & a)
Do
a = Dir
If a <> "" Then
Workbooks.Open ("C:\Users\Administrator\Desktop\新建文件夹 (3)\" & a)
Else
Exit Do
End If
Loop
For Each wk In Workbooks
a = wk.Name
If wk.Name <> "汇总.xlsm" Then
wk.Activate
For Each wsh In Worksheets
arr = wsh.Range("a2:e" & wsh.Range("e65536").End(xlUp).Row)
k = k + UBound(arr)
ReDim Preserve brr(1 To 6, 1 To k)
For i = 1 To UBound(arr)
n = n + 1
brr(1, n) = arr(i, 1)
brr(2, n) = arr(i, 2)
brr(3, n) = arr(i, 3)
brr(4, n) = arr(i, 4)
brr(5, n) = arr(i, 5)
brr(6, n) = wk.Name
Next i
Next
End If
Next
Sheet1.Range("a2").Resize(n, 6) = Application.Transpose(brr)
Sheet1.Range("f2:f" & Sheet1.Range("f65536").End(xlUp).Row).Replace What:=".xlsm", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
For Each wk In Workbooks
If wk.Name <> "汇总.xlsm" Then
wk.Activate
wk.Close True
End If
Next
End Sub
|
|