How to Use VBA Code to Merge Multiple Excel Sheets
Explain clearly how to use the VBA code for merging multiple Excel tables: when a macro is suitable, how to enable macros and paste the code, which places to change before running it, how to check the results after merging, and a reminder to back up the original files first.
Key points of this article
- When there are many tables with identical headers and column order that need repeated merging, a VBA macro is more efficient than manual copy and paste.
- Running the macro modifies the table, so back up the original file first and save it in macro-enabled workbook format.
- The code creates a new worksheet named Merged Results; the contents of the original worksheets remain unchanged.
- The number of header rows and the result sheet name can be adjusted to suit your own tables; test-run on a copy before making any changes.
- After merging, verify the total row count, headers, and column alignment; if misalignment is found, first standardize the structure of each table.
Many people have a dozen or so monthly reports with identical structures and want to merge them into one master sheet, but they can only copy and paste them one by one. Manual work is slow, and it's easy to miss rows or paste into the wrong place. Excel's VBA macros can hand this job over to a program, so that a single run produces the consolidated result.
This article is for readers who already know basic spreadsheet operations but have never written code. By the end, you will know when VBA is the right tool, how to paste code into Excel and run it, which parts you need to change to fit your own spreadsheet before running it, and how to check the results after merging.
Before you start, one thing to keep in mind: macros operate directly on the data in the workbook, and after they run you usually cannot undo the changes the way you would with ordinary edits, so be sure to back up the original file first.
Should You Use VBA Code to Merge Multiple Sheets? The Short Answer
If there are only two or three tables, manually copying and pasting is usually faster. When there are many tables and each table's headers and column order are consistent, using a macro is worthwhile. The table below can help you decide.
| Your situation | Recommended Practices | Cause |
|---|---|---|
| There are only two or three tables, and their structures are different. | Manually copy and paste, or use Excel's built-in Data query feature. | Few steps; no need to enable macros. |
| There are many tables, and their column structure is consistent. Merge them only once. | VBA macro | It can be completed in a single run, with no need for repeated operations. |
| Many tables with a consistent structure; they need to be merged every month. | VBA macro, and keep the code | Next time, just replace the original table and run it once more. |
| The document comes from an external source; macros are prohibited from being enabled on company computers. | Merge manually, or ask an administrator to confirm permissions. | Hong may be blocked by the security policy |
| The table contains formulas and formatting that need to be preserved. | VBA macro with manual verification | Copying will carry the formulas and formatting along with it, and references may change. |
Before You Begin: Confirm These Points First
- Table structure consistency. This code assumes that the first row of each table is a header: the first table keeps its header, and the remaining tables skip their first row. If the column order differs between tables, the merged data will be misaligned, so the column order should be standardized first.
- Blank rows in column A. The code determines the number of data rows from the last non-empty cell in column A. If there are blank rows in the middle of column A, the data below them will be missed. Before merging, fill in or delete these blank rows.
- File format. A workbook containing macros must be saved in .xlsm format; a regular .xlsx file cannot save macro code.
- Macro security settings. If a security warning appears when opening the file, you need to confirm enabling content. Company computers may restrict macros; in that case, first confirm your permissions rather than bypassing the restriction.
- Backup. Make a copy of the original file and include the date in the file name. If something goes wrong, you can use the backup to start over.
Steps to Paste and Run the Merge Macro in Excel
- Back up the original file: Copy the workbook and place the copy in another folder. All subsequent operations will be performed on the copy.
- Save as a macro-enabled format: In the Save As dialog, change the file type to macro-enabled workbook (.xlsm).
- Open the VBA editor: In Excel, press Alt+F11 to open the VBA editor window.
- Insert a standard module: In the Project Explorer on the left, right-click the current workbook and choose Insert > Module. A blank code window will appear.
- Paste the code: Paste the code below into the module window line by line. VBA is not sensitive to indentation, but the order of statements must not be changed.
- Run the macro: Return to Excel, press Alt+F8, select the macro named Merge Worksheets, and click Run. A message box will appear when the run finishes.
- Check the results: A merged result worksheet will be added in front of the workbook. Verify each item one by one (see the next section).
The merged code is as follows, with each line corresponding to one statement:
- Sub Merge Worksheets()
- Dim ws As Worksheet, dest As Worksheet
- Dim lastRow As Long, startRow As Long, nextRow As Long
- Dim firstSheet As Boolean
- firstSheet = True
- nextRow = 1
- Set dest = ThisWorkbook.Worksheets.Add(Before:=ThisWorkbook.Worksheets(1))
- dest.Name = "Merge Result"
- For Each ws In ThisWorkbook.Worksheets
- If ws.Name <> dest.Name Then
- lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
- If firstSheet Then startRow = 1 Else startRow = 2
- If lastRow >= startRow Then
- ws.Range(ws.Cells(startRow, 1), ws.Cells(lastRow, ws.UsedRange.Columns.Count)).Copy dest.Cells(nextRow, 1)
- nextRow = nextRow + (lastRow - startRow + 1)
- firstSheet = False
- End If
- End If
- Next ws
- MsgBox "Merge completed"
- End Sub
If your header spans two rows, change "startRow = 2" in the code to "startRow = 3". To change the name of the result sheet, edit the text inside the quotes on the "dest.Name" line. These are the two places that most often need adjusting.
Verify each item after completion
Completing the macro run does not necessarily mean the results are correct. It is recommended to verify at least the following items.
| Checklist items | How do you see it? | What should I do if I have a problem? |
|---|---|---|
| Total rows | Add up the number of data rows in each source table, then compare the total with the number of rows in the result table. | If there are too few rows, check whether column A has blank rows; if there are too many, check whether the header row was carried over (duplicated) into the data. |
| Table header | Check whether the first row of the result table appears only once. | If the table header is repeated, check whether the startRow setting is correct. |
| Column alignment | Spot-check the first and last rows of each table to see whether the data falls in the correct columns. | If the columns are misaligned, return to the original table, unify the column order, and then run it again. |
| Numbers and Dates | Check whether the numbers can still be summed, and whether the dates still display as dates. | If converted to text, you can use general methods such as Text to Columns or Paste Special (Values) to standardize the format. |
| Original table content | Confirm that the original worksheet has not been modified | If it has been modified, restore it from the backup file and then perform the operation again. |
If the results are unsatisfactory, do not force changes onto the results table. Instead, go back to the backup file, correct the original table structure, and then rerun the macro.
📝 Summary
The VBA code for merging multiple Excel sheets suits scenarios with many sheets, a uniform structure, and merges that need to be repeated. Before you start, back up the original file. Save the file in a macro-enabled format, paste in the code, and make necessary adjustments based on the number of header rows and the sheet names. After running it, check the row counts, headers, and column alignment item by item, and only use the result once it is confirmed to be correct.
❓ Frequently Asked Questions (FAQ)
1. Can data be undone after a macro runs?
You generally cannot recover using undo the way you would in a normal editor, so be sure to back up the original file before running anything. If something goes wrong, simply redo the operation from the backup.
2. Will the code still work if each table has a different name?
Yes, that works. The code iterates through all worksheets in the workbook, and sheet names do not affect the merge. Note that the name of the result sheet must not duplicate the name of any existing worksheet.
3. What should I do if a message says macros are disabled when I open a file?
First confirm that the file comes from a reliable source, then enable the content as prompted. If it is a company computer, it may be subject to a unified security policy, so check your permissions with the administrator and do not try to bypass any restrictions.
4. I can't write code. Can I use this code directly?
You can follow the steps and paste and run the code, but make sure you understand each place that needs to be modified, such as the number of header rows and the table names. As long as the table structure is consistent, the code logic usually does not need to be changed.
5. Is the data wrong after merging a code problem?
In most cases the problem lies in the original table, for example inconsistent column order, blank rows in column A, or a different number of header rows. First check each item against the verification checklist, then decide whether the code needs to be modified.


