如何将多个Excel文件合并成一个且保留原有数据?
如何将多个Excel文件合并成一个且保留原有数据?事实上,我们可以借用Excel宏命令来处理该操作,并且保留原有数据,建立多个Sheet页,具体操作请看下文多个Excel文件合并成一个的方法。
如何将多个Excel文件合并成一个且保留原有数据?
1、将需合并的Excel文件放在同一个文件夹中,并这个文件中新建一个Excel文件。
2、打开新建的Excel文件,按 Alt + F11 键,打开宏,选择视图→代码窗口。
3、将下面的代码拷贝进去:
- 01Sub sheets2one()
- 02'定义对话框变量
- 03Dim cc As FileDialog
- 04Set cc = Application.FileDialog(msoFileDialogFilePicker)
- 05Dim newwork As Workbook
- 06Set newwork = Workbooks.Add
- 07With cc
- 08If .Show = -1 Then
- 09Dim vrtSelectedItem As Variant
- 10Dim i As Integer
- 11i = 1
- 12For Each vrtSelectedItem In .SelectedItems
- 13Dim tempwb As Workbook
- 14Set tempwb = Workbooks.Open(vrtSelectedItem)
- 15tempwb.Worksheets(1).Copy Before:=newwork.Worksheets(i)
- 16newwork.Worksheets(i).Name = VBA.Replace(tempwb.Name, ".xls", "")
- 17tempwb.Close SaveChanges:=False
- 18i = i + 1
- 19Next vrtSelectedItem
- 20End If
- 21End With
- 22Set cc = Nothing
- 23End Sub
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文件,将工作簿另存为即可。
以上步骤便是,合并后的Excel文档会献出多个Sheet页,我们可以很方便的切换了解。
{else}如何将多个Excel文件合并成一个且保留原有数据?事实上,我们可以借用Excel宏命令来处理该操作,并且保留原有数据,建立多个Sheet页,具体操作请看下文多个Excel文件合并成一个的方法。
如何将多个Excel文件合并成一个且保留原有数据?
1、将需合并的Excel文件放在同一个文件夹中,并这个文件中新建一个Excel文件。
2、打开新建的Excel文件,按 Alt + F11 键,打开宏,选择视图→代码窗口。
3、将下面的代码拷贝进去:
- 01Sub sheets2one()
- 02'定义对话框变量
- 03Dim cc As FileDialog
- 04Set cc = Application.FileDialog(msoFileDialogFilePicker)
- 05Dim newwork As Workbook
- 06Set newwork = Workbooks.Add
- 07With cc
- 08If .Show = -1 Then
- 09Dim vrtSelectedItem As Variant
- 10Dim i As Integer
- 11i = 1
- 12For Each vrtSelectedItem In .SelectedItems
- 13Dim tempwb As Workbook
- 14Set tempwb = Workbooks.Open(vrtSelectedItem)
- 15tempwb.Worksheets(1).Copy Before:=newwork.Worksheets(i)
- 16newwork.Worksheets(i).Name = VBA.Replace(tempwb.Name, ".xls", "")
- 17tempwb.Close SaveChanges:=False
- 18i = i + 1
- 19Next vrtSelectedItem
- 20End If
- 21End With
- 22Set cc = Nothing
- 23End Sub
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文件,将工作簿另存为即可。
以上步骤便是,合并后的Excel文档会献出多个Sheet页,我们可以很方便的切换了解。