Copy Worksheets from Multiple Excel Workbooks into a Master Workbook
Question details
The user needs to combine worksheets from multiple Excel workbooks into a master workbook using VBA.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A macro processes workbooks in a folder and copies their worksheets into one master workbook.
- Observed behavior
- Only the final workbook is copied, closing a source workbook causes an automation error, and the combined workbook needs to be saved correctly.
Save and open the master workbook as an Excel Macro-Enabled Workbook (.xlsm), and close the source workbooks before running the macro. Check the source folder path and make sure you can open the files and add sheets to the master workbook.
Copy each worksheet directly into the master workbook with VBA
Use explicit workbook references to copy every source worksheet, close each source without saving, and save the master after the loop.
This example assumes the macro is stored in the master workbook, so ThisWorkbook identifies the destination. The source files remain unchanged.
Open the master workbook in Excel, then select Developer > Visual Basic. In the VBA editor, select Insert > Module.
Paste this code into the module: Sub CombineWorksheets() Dim masterWorkbook As Workbook Dim sourceWorkbook As Workbook Dim ws As Worksheet Dim folderPath As String Dim fileName As String Set masterWorkbook = ThisWorkbook folderPath = "C:\Assessments" & Application.PathSeparator fileName = Dir(folderPath & "*.xls*") Do While fileName <> "" If Left(fileName, 2) <> "~$" And _ StrComp(fileName, masterWorkbook.Name, vbTextCompare) <> 0 Then Set sourceWorkbook = Workbooks.Open( _ Filename:=folderPath & fileName, UpdateLinks:=0, ReadOnly:=True) For Each ws In sourceWorkbook.Worksheets ws.Copy After:=masterWorkbook.Sheets(masterWorkbook.Sheets.Count) Next ws sourceWorkbook.Close SaveChanges:=False End If fileName = Dir() Loop masterWorkbook.Save End Sub
Replace C:\Assessments in the folderPath line with the path to your source folder. The example is a Windows path; use a valid path for your system.
In Excel, select Developer > Macros, choose CombineWorksheets, and select Run. Check the sheet tabs in the master workbook after the macro finishes.

Work with Excel-Compatible Spreadsheets in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar interface and support for common Excel formats, making it easier to move spreadsheet work between applications. VBA support can vary, so check macro compatibility before migrating this workflow.

Frequently Asked Questions
Why should I avoid ActiveSheet in the copy macro?
ActiveSheet can change when a workbook opens or a sheet is copied. Use ws.Copy with an explicit destination such as masterWorkbook.Sheets(masterWorkbook.Sheets.Count) instead.
When should the master workbook be saved?
Call masterWorkbook.Save after the folder-processing loop finishes so the copied worksheets are saved together. Save the macro-containing master as an .xlsm file first.
Will the macro change the source workbooks?
The example opens each source with ReadOnly:=True and closes it with SaveChanges:=False, so it does not save changes to the source files.
What if a worksheet cannot be copied?
Check whether the source file opens normally and whether the master workbook's structure is protected. In Excel, review the protection settings on the Review tab, then run the macro again.




