1 将需合并的excel文件放在同一个文件夹中,并这个文件中新建一个excel文件
2 打开新建的excel文件,按alt + f11建,打开宏,选择视图——代码窗口
3 将下面分割线内的的代码拷贝进去,并保存。
======我是分割线======
Sub sheets2one()
'定义对话框变量
Dim cc As FileDialog
Set cc = Application.FileDialog(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)
tempwb.Worksheets(1).Copy Before:=newwork.Worksheets(i)
newwork.Worksheets(i).Name = VBA.Replace(tempwb.Name, ".xls", "")
tempwb.Close SaveChanges:=False
i = i + 1
Next vrtSelectedItem
End If
End With
Set cc = Nothing
End Sub
======我是分割线======
4 点击运行,弹出对话框可,点击运行选项运行程序,
5 再次弹出选择对话框,选择要批量合并的excel名,确定即可
6 程序运行完毕后,会自动生成一个工作簿,工作簿内包含所有需合并的excel文件,将工作簿另外即可