怎样编写一个VBA,快速将多个excel工作簿按相同名称的工作表分开汇总到一个新表格里。

2024-10-31 01:14:39
推荐回答(1个)
回答1:

'把要汇总的工作簿放在同一文件夹下,再建一个汇总工作簿,把下面代码放入汇总工作簿中。
Sub ABCD()
Dim lj As String
Dim dirname As String
Dim nm As String
lj = ActiveWorkbook.Path
nm = ActiveWorkbook.Name
dirname = Dir(lj & "\*.xls")
Cells.Clear
Do While dirname <> ""
If dirname <> nm Then
Workbooks.Open Filename:=lj & "\" & dirname
Workbooks(nm).Activate
Workbooks(dirname).Sheets(1).Range("A4:J15").Copy _
Sheets(1).Range("a65536").End(xlUp).Offset(1, 0)
Workbooks(dirname).Close False
End If
dirname = Dir
Loop
Rows("1:1").Select
Selection.Delete Shift:=xlUp
End Sub