How to Combine Excel Workbooks into a Master Worksheet using VBA
Question details
Consolidate data from a specific worksheet across multiple Excel workbooks into one centralized master worksheet.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Gathering standardized reports or logs spread across multiple individual Excel files in a folder into a single master workbook for comprehensive analysis.
- Observed behavior
- Automating the repetitive task of opening, copying, and pasting data from numerous workbooks into one master sheet to save time and reduce manual errors.
Gather all the Excel workbooks you want to combine and place them into a single, dedicated folder on your computer to ensure the macro processes the correct files without interruption.
Use a VBA Macro to Combine Workbooks
Automate the consolidation process by running a VBA script that automatically loops through a designated folder, extracts data, and appends it to your master sheet.
This method is highly efficient for recurring tasks. The script will open each file, copy the used range from a specific sheet (e.g., 'Comment'), find the next available blank row in your 'Master Comment' sheet, paste the values, and close the source file.
Create a new master workbook in Excel. Press the Alt + F11 keys simultaneously on your keyboard to launch the Microsoft Visual Basic for Applications (VBA) editor.
In the VBA editor, click on 'Insert' from the top menu bar, and then select 'Module' to create a blank workspace for your script.
Paste your VBA code into the module window. Locate the line that defines the folder path (e.g., myPath = "C:\Users\Desktop\Workbooks") and update it to the exact folder path on your PC where your files are stored. Also, ensure the sheet names in the code match your actual sheet names.
Click anywhere inside the pasted code and press the F5 key, or click the green 'Run' arrow in the toolbar. The macro will execute, rapidly pulling all data into your master worksheet.

Combine Workbooks Using Power Query
If you prefer a solution that requires no coding, Excel's Power Query feature is an extremely powerful and visual alternative to combining files in a folder.
Combine Multiple Workbooks Easily with WPS Spreadsheet
WPS Office fully supports VBA macros and advanced data management tools, allowing you to run automated consolidation scripts just as seamlessly as you would in Microsoft Excel.
- 1. Open WPS Spreadsheet: Launch WPS Office and create a new blank spreadsheet to serve as your master workbook.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon. If you do not see it, go to Settings to enable the Developer tab.
- 3. Open the VBA Editor: Click on 'VBA Editor' to open the scripting window. Go to Insert > Module.
- 4. Execute the Consolidation Script: Paste your data-combining VBA code, update your target folder path, and press F5 to compile your multi-workbook data into the master sheet.

Frequently Asked Questions
Why is the macro not combining all the files in my folder?
This usually happens due to a folder path mismatch or a file extension issue. Ensure the path in your code exactly matches your directory and ends correctly without a trailing backslash unless specified. Also, verify that the Dir() function in your code is looking for the correct extension (e.g., "*.xlsx" instead of "*.xls").
Can I combine worksheets if they have different sheet names?
Yes, but you will need to modify the VBA code. Instead of referencing a specific name like wb.Sheets("Comment"), you can use an index number, such as wb.Sheets(1), to consistently copy the first worksheet in every workbook regardless of its specific name.
How do I prevent the macro from overwriting existing data in the master sheet?
Ensure your VBA code calculates the next available empty row dynamically before pasting. Using a command like lastRow = wsMaster.Cells(wsMaster.Rows.Count, 1).End(xlUp).Row + 1 ensures the script appends new data sequentially below the existing records.
Is it possible to stop the macro from flashing the screen while it runs?
Yes, screen flashing can significantly slow down your macro. You can prevent this by adding the line Application.ScreenUpdating = False at the very beginning of your VBA script, and Application.ScreenUpdating = True at the end.




