代碼邏輯如下:
Sub 合並工作薄() On Error Resume Next Dim SummarySheet As Worksheet '匯總表 Dim SelectedFiles() As Variant '選擇的文件集合 Dim Nrow As Long Dim FileName As String Dim NFile As Long Dim WorkBk As Workbook Dim SourceRange As Range Dim DestRange As Range Set SummarySheet = ThisWorkbook.Worksheets(1) SelectedFiles = Application.GetOpenFilename(filefilter:="Excel 文件(*.xl*),*.xl*", MultiSelect:=True) Nrow = 1 For NFile = LBound(SelectedFiles) To UBound(SelectedFiles) FileName = SelectedFiles(NFile) Set WorkBk = Workbooks.Open(FileName) Set SourceRange = WorkBk.Worksheets(1).UsedRange Set DestRange = SummarySheet.Range("A" & Nrow) Set DestRange = DestRange.Resize(SourceRange.Rows.Count, SourceRange.Columns.Count) DestRange.Value = SourceRange.Value Nrow = Nrow + DestRange.Rows.Count WorkBk.Close savechanges:=False Next SummarySheet.Columns.AutoFit End Sub
說明:這里是分別復制給子工作簿/工作表的值,而沒有復制格式,如果需要特殊格式的,可以現在匯總表中進行格式設置,再執行合並動作。
運行宏代碼,選擇工作表:

