如果你有多个Excel 文件希望可以合到同一个文件中,可以使用如下 VBA 代码:
1. 创建一个 Excel 文件
2. 使用 ALT+F11 打开 VBA 界面
3. 输入如下代码
Sub mergeFiles()
'Merges all files in a folder to a main file.
'Define variables:
Dim numberOfFilesChosen, i As Integer
Dim tempFileDialog As fileDialog
Dim mainWorkbook, sourceWorkbook As Workbook
Dim tempWorkSheet As Worksheet
Set mainWorkbook = Application.ActiveWorkbook
Set tempFileDialog = Application.fileDialog(msoFileDialogFilePicker)
'Allow the user to select multiple workbooks
tempFileDialog.AllowMultiSelect = True
numberOfFilesChosen = tempFileDialog.Show
'Loop through all selected workbooks
For i = 1 To tempFileDialog.SelectedItems.Count
'Open each workbook
Workbooks.Open tempFileDialog.SelectedItems(i)
Set sourceWorkbook = ActiveWorkbook
'Copy each worksheet to the end of the main workbook
For Each tempWorkSheet In sourceWorkbook.Worksheets
tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
Next tempWorkSheet
'Close the source workbook
sourceWorkbook.Close
Next i
End Sub
4. 运行之后会弹出对话框选择要合并的excel文件目录(特别注意目录中不要含有中文)
5. 最后生成的文件中包含了前面目录中的所有文件内容,每一个文件是单独存放在一个 Sheet 页面中的。
此外,还可以使用在线合并服务,例如:https://products.aspose.app/cells/merger (每次不能超过10个文件)
还可以开通 WPS 会员进行合并。
来源: