操作系统:win10
操作软件:Excel2016(Excel 2021也支持)
1、将需合并的Excel文件放在同一个文件夹中。
2、打开新建的Excel文件,按 Alt + F11 键,打开宏,选择视图→代码窗口。
3、将下面的代码拷贝粘贴到代码窗口中:
Sub sheets2one()
'定义对话框变量
Dim cc As FileDialog
Set cc = A(msoFileDialogFilePicker)
Dim newwork As Workbook
Set newwork = Workbooks.Add
With cc
If .Show = -1 Then
Dim vrtSelectedItem As Variant
Dim i As Integer
i = 1
For Each vrtSelectedItem In .SelectedItems
Dim tempwb As Workbook
Set tempwb = Workbooks.Open(vrtSelectedItem)
(1).Copy Before:=newwork.Worksheets(i)
newwork.Worksheets(i).Name = VBA.Replace, ".xls", "")
SaveChanges:=False
i = i + 1
Next vrtSelectedItem
End If
End With
Set cc = Nothing
End Sub
4、点击菜单“运行”—“运行子过程/用户窗体”。
5、在弹出选择对话框中,选择要批量合并的Excel文件,点击“确定”即可合并为一个Excel工作簿。
6、点击“确定”后,运行合并即可。