VBA & Macro Problems

Copy Worksheets from Multiple Excel Workbooks into a Master Workbook

Elise WilliamsElise Williams Dec 2, 2026 868 views

Question details

The user needs to combine worksheets from multiple Excel workbooks into a master workbook using VBA.

Copy Worksheets from Multiple Excel Workbooks into a Master Workbook
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

Open the master workbook in Excel, then select Developer > Visual Basic. In the VBA editor, select Insert > Module.

2
Add the copy macro

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

3
Set the source folder

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.

4
Run and check the result

In Excel, select Developer > Macros, choose CombineWorksheets, and select Run. Check the sheet tabs in the master workbook after the macro finishes.

Copy each worksheet directly into the master workbook with VBA
If the macro still stops: Check whether a source file requires a password or the master workbook's structure is protected. An automation error can have other causes, so note the highlighted line and error message when troubleshooting.
Free Microsoft Office alternative

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.

Open and edit spreadsheets in a familiar interfaceCompatible with common Microsoft Excel formats, including XLSX and XLSManage multiple worksheets in WPS SpreadsheetFree and lightweight for everyday Office tasks
microsoft office alternative - wps office

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.