快速合并多个 Excel 工作簿成为一个工作簿

有些应用场景需要合并Excel中的sheet表格。

#将多个Workbook中的sheets合并到一个Book中(新建一个book,并执行下列代码)
Sub Workbook_merge()
Rem This script is used to collect worksheets of serval workbooks into one workbook!

Dim FileOpen
Dim X As Integer
Dim Wb As Workbook
Dim sh As Worksheet
Application.ScreenUpdating = False
FileOpen = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbook(*.xlsx),*.xlsx", MultiSelect:=True, Title:="Please select the Workbooks you want to merge:")
X = 1
Application.DisplayAlerts = False
While X <= UBound(FileOpen)
      Set Wb = GetObject(FileOpen(X))
      For Each sh In Wb.Sheets
          If Application.WorksheetFunction.CountA(sh.Cells) <> 0 Then
             sh.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
          End If
      Next
      Wb.Close SaveChanges:=False
      X = X + 1
Wend
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.ScreenUpdating = True
End Sub
#合并一个Book中的多个Sheets到当前sheet的代码(自动忽略空白Sheets)
Sub Sheet_merge()
Rem This Script can be used to merge all worksheets into current worksheet!
   Application.ScreenUpdating = False  
   For j = 1 To Sheets.Count
       If Sheets(j).Name <> ActiveSheet.Name Then
          X = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count
          Sheets(j).UsedRange.Copy ActiveSheet.Cells(X, 1)
       End If      
   Next
   Application.ScreenUpdating = True
   MsgBox "All sheets have been merged!", vbInformation, "Attention"
End Sub
#合并Excel,并以文件名为sheet名
'功能:把多个excel工作簿的第一个sh eet工作表合并到一个excel工作簿的多个sheet工作表,新工作表的名称等于原工作簿的名称
Sub Books2Sheets()
'定义对话框变量
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'新建一个工作簿
Dim newwb As Workbook
Set newwb = Workbooks.Add
With fd
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:=newwb.Worksheets(i)
'把新工作簿的工作表名字改成被复制工作簿文件名,这儿应用于xls文件,即Excel97-2003的文件,如果是Excel2007,需要改成xlsx 注意!这里是英文的双引号!否则语法编译错误
newwb.Worksheets(i).Name = VBA.Replace(tempwb.Name, ".xlsx", "")
'关闭被合并工作簿
tempwb.Close SaveChanges:=False
i = i + 1
Next vrtSelectedItem
End If
End With
Set fd = Nothing
End Sub
#excel合并到同一工作簿工作表,自动去掉重复表头。多工作薄合并到一工作薄同一工作表下。
#数据超过了65536行。只用把sht.[a1].CurrentRegion.Offset(1).Copy sh.[a65536].End(xlUp).Offset(1); (只要你用的是Excel 2003以后的版本)改成sht.[a1].CurrentRegion.Offset(1).Copy sh.[a1048576].End(xlUp).Offset(1)就好。
#表格格式是“xlsx”,然后就把代码中的“xls”替换成表格格式。
Sub Macro1()
Dim MyPath$, MyName$, sh As Worksheet, sht As Worksheet, m&
Set sh = ActiveSheet
MyPath = ThisWorkbook.Path & "\"
MyName = Dir(MyPath & "*.xls")
Application.ScreenUpdating = False
Cells.ClearContents
Do While MyName <> ""
If MyName <> ThisWorkbook.Name Then
With GetObject(MyPath & MyName)
For Each sht In .Sheets
If IsSheetEmpty = IsEmpty(sht.UsedRange) Then
m = m + 1
If m = 1 Then
sht.[a1].CurrentRegion.Copy sh.[a1]
Else
sht.[a1].CurrentRegion.Offset(1).Copy sh.[a65536].End(xlUp).Offset(1)
End If
End If
Next
.Close False
End With
End If
MyName = Dir
Loop
Application.ScreenUpdating = True
End Sub

参考链接:
https://www.zhihu.com/question/20366713
https://blog.csdn.net/ZeroBz/article/details/102856403
https://www.jianshu.com/p/c8ae6852f1da

本文链接:

https://martin.cool/archives/382.html
1 + 2 =
快来做第一个评论的人吧~