To combine multiple Excel workbooks with VBA, you write a macro that loops through every file in a folder, opens each one, copies its used range into a single master sheet, and closes the source file without saving. The core pattern is a Dir loop plus Workbooks.Open and Range.Copy; the whole job is about 25 lines of code. This guide gives you a working macro, explains each part, and covers the folder layout, header handling and error traps that decide whether the merge runs cleanly or duplicates rows.
What You'll Need
- Excel on Windows or Mac with macros enabled (the VBA editor is included in every desktop edition; Excel for the web cannot run VBA).
- All source workbooks saved as .xlsx, .xlsm or .xls in one folder, with no other files mixed in.
- Identical column layout across the source files — same headers in the same order on the same sheet name.
- A blank master workbook to hold the combined data, saved as .xlsm so the macro stays with it.
- Permission to trust the folder: if macros are blocked, enable them for that location via File > Options > Trust Center > Trust Center Settings > Trusted Locations.
Step-by-Step
- Set up the folder. Create a folder such as C:\Merge\Source and put only the workbooks you want combined in it. The macro will process every file it finds, so remove backups, shortcuts and the master file itself.
- Open the master workbook and the VBA editor. Press Alt+F11 (on Mac, Fn+Alt+F11) to open the editor. In the Project pane, right-click your workbook and choose Insert > Module.
- Paste the macro. Use this code, adjusting the folder path and sheet name to match your files:
Sub CombineWorkbooks() Dim folderPath As String, fileName As String Dim wsDest As Worksheet, wbSource As Workbook Dim nextRow As Long folderPath = "C:\Merge\Source\" Set wsDest = ThisWorkbook.Sheets("Combined") nextRow = 1 fileName = Dir(folderPath & "*.xlsx") Application.ScreenUpdating = False Application.DisplayAlerts = False Do While fileName <> "" Set wbSource = Workbooks.Open(folderPath & fileName, ReadOnly:=True) With wbSource.Sheets(1).UsedRange If nextRow = 1 Then .Copy wsDest.Cells(1, 1) nextRow = wsDest.Cells(wsDest.Rows.Count, 1).End(xlUp).Row + 1 Else .Offset(1, 0).Resize(.Rows.Count - 1).Copy _ wsDest.Cells(nextRow, 1) nextRow = wsDest.Cells(wsDest.Rows.Count, 1).End(xlUp).Row + 1 End If End With wbSource.Close SaveChanges:=False fileName = Dir Loop Application.ScreenUpdating = True Application.DisplayAlerts = True MsgBox "Done. Rows written: " & nextRow - 1 End Sub - Create the destination sheet. Add a sheet named Combined to the master workbook. The macro writes the first file's headers into row 1 and appends every later file below, skipping each file's header row.
- Check the file extension. The line
Dir(folderPath & "*.xlsx")only picks up .xlsx files. If your sources are .xlsm or .xls, change the pattern to*.xls*so all Excel formats are caught. - Run it. Close any open source workbooks, then press F5 in the editor or run the macro from Developer > Macros. Screen updating is switched off during the loop, so the screen may look frozen; a message box appears when it finishes.
- Verify the result. Compare the row count in the message box with the total rows across your source files (minus one header row per file). Spot-check the last few rows to confirm nothing was truncated.
Common Mistakes
- Leaving the master in the source folder. The macro will open the master, copy its own data back into itself and loop until it runs out of memory. Always keep the master outside the source folder.
- Mismatched headers. If one file has an extra column or a different order, the appended rows land under the wrong headings. Standardise the templates before merging.
- Forgetting the header skip. Copying the full used range from every file repeats the header row between each block. The
Offset(1, 0).Resize(.Rows.Count - 1)line is what prevents this. - Hard-coding the wrong sheet.
Sheets(1)means the first tab, not necessarily the data tab. If your data sits on a named sheet, usewbSource.Sheets("Data"). - Hidden rows and blank columns.
UsedRangeincludes formatted but empty cells, which can inflate the copied area. Clear stray formatting in the source files first.
Tips
- Add a source column so you can trace each row: before copying, write
wsDest.Cells(nextRow, wsDest.Columns.Count).End(xlToLeft).Offset(0, 1).Value = fileNameand reserve a header for it. - For very large folders, turn off
Application.Calculationas well as screen updating, then set it back toxlCalculationAutomaticat the end. - Test on a copy of the folder first. The macro opens files read-only and closes without saving, but a mistake in the destination range can still overwrite master data.
- If the source files are password-protected, the macro will prompt for each password. Remove protection or supply the password in the
Opencall if you have it. - Keep a version of the macro that writes to a new workbook each run, so you never destroy the previous merge.
Troubleshooting
The macro stops with “Subscript out of range”. The sheet named Combined does not exist in the master, or a source file lacks the expected sheet. Check the tab names exactly, including spaces.
No files are found. The folder path is wrong or missing its trailing backslash, or the extension pattern does not match your files. Print fileName in the Immediate window to see what Dir returns.
Only the first file is copied. The fileName = Dir line inside the loop is missing or misplaced; without it the loop never advances.
Excel freezes or runs out of memory. The master is being picked up as a source file, or a source workbook is already open and locked. Move the master out of the folder and close all other workbooks.
Rows are duplicated. The header-skip logic is copying the first row of each file twice, usually because nextRow was not updated after the copy. Recalculate it from the last used row each time.
Macros are disabled. Enable them for the file or add the folder to Trusted Locations; otherwise the code will not run at all.
Frequently Asked Questions
Can this combine .xls and .xlsm files too?
Yes. Change the Dir pattern to *.xls*. The macro opens each format the same way, though .xlsm files may trigger a macro warning on open.
Does it work on Excel for Mac?
Yes, with two changes: use forward slashes or the Mac path format in folderPath, and open the editor with Fn+Alt+F11. Excel for the web cannot run VBA at all.
How do I merge only specific sheets?
Replace Sheets(1) with the sheet name, for example Sheets("Sales"). If a file lacks that sheet, the macro errors, so add an On Error Resume Next check or verify the files first.
Will it overwrite my existing data?
It writes starting at row 1 of the Combined sheet, so clear that sheet before each run. The source files are opened read-only and closed without saving, so they are untouched.
Can I combine files from subfolders?
Not with a single Dir loop. You need a recursive routine that walks each subfolder, or move all files into one flat folder first.