1、把需要合并的excel表格文档放到同一个文件夹里
2、新建一个“数据合并.xlsx“文档
3、打开“数据合并.xlsx“文档,在”Sheet1“工作表的地方右键→查看代码(快捷键:“Alt+F11”,尽量选用快)进入到microsoft Visual Basic for Applications窗口
4、双击工程资源管理器里面的sheet1,在右侧的代码区粘贴如下代码:
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 & "\" & "*.csv")
AWbName = ActiveWorkbook.Name
Num = 0
Do While MyName <> ""
If MyName <> AWbName Then
Set Wb = Workbooks.Open(MyPath & "\" & MyName)
Num = Num + 1
IF Num=1 Then
With Workbooks(1).ActiveSheet
For G = 1 To Sheets.Count
Wb.Sheets(G).UsedRange.Copy .Cells(.Range("A65536").End(xlUp).Row , 1)
Next
WbN = WbN & Chr(13) & Wb.Name
Wb.Close False
End With
Else
With Workbooks(1).ActiveSheet
For G = 1 To Sheets.Count
Wb.Sheets(G).UsedRange.Copy .Cells(.Range("A2").End(xlDown).Row +1 , 1)
Next
WbN = WbN & Chr(13) & Wb.Name
Wb.Close False
End With
End if
End If
MyName = Dir
Loop
Range("A1").Select
Application.ScreenUpdating = True
MsgBox "共合并了" & Num & "个工作薄下的全部工作表。如下:" & Chr(13) & WbN,vbInformation, "提示"
End Sub
5、运行→运行子过程/用户窗体(或者直接点击运行按钮,快捷键:F5),即可合并所有Excel表格到”数据合并.xlsx“文档的Sheet1工作表里面
注意:如果合并出现问题可能是因为代码部分excel文件后缀不同导致,修改代码MyName部分即可。