合并同一目录下多个EXCEL的多个sheet

时间:2022-01-26 18:54:12

合并同一目录下多个EXCEL的多个sheet到一个excel的一个sheet

1、把多个excel表都放在同一个文件夹里面,并在这个文件夹里面新建一个excel
2、打开新建的excel表,并右键单击sheet1,找到“查看代码”,单击进去

3、贴入下面代码

Sub 合并当前目录下所有工作簿的全部工作表()
Dim MyPath, MyName, AWbName
Dim Wb As Workbook, WbN As String
Dim G As Long
Dim Num As Long
Dim BOX As String
Application.ScreenUpdating = False
MyPath = ActiveWorkbook.Path
MyName = Dir(MyPath & "\" & "*.xls")
AWbName = ActiveWorkbook.Name
Num =
Do While MyName <> ""
If MyName <> AWbName Then
Set Wb = Workbooks.Open(MyPath & "\" & MyName)
Num = Num +
With Workbooks().ActiveSheet
.Cells(.Range("B65536").End(xlUp).Row + , ) = Left(MyName, Len(MyName) - )
For G = To Sheets.Count
Wb.Sheets(G).UsedRange.Copy .Cells(.Range("B65536").End(xlUp).Row + , )
Next
WbN = WbN & Chr() & Wb.Name
Wb.Close False
End With
End If
MyName = Dir
Loop
Range("B1").Select
Application.ScreenUpdating = True
MsgBox "共合并了" & Num & "个工作薄下的全部工作表。如下:" & Chr() & WbN, vbInformation, "提示"
End Sub

4、执行