We have a workbook consisting of 3 worksheets, Sheet1, Sheet2, and Sheet3. Each contains the sales record of some products for 3 different weeks.
Sheet1 contains the sales record of Week 1.
Sheet2 contains the sales record of Week 2.
Sheet3 contains the sales record of Week 3.
We will merge this data into a single sheet.
Method 1 – Merge Data Sets from Multiple Sheets Into One Sheet with VBA Row-wise
⧭ VBA Code:
Sub Merge_Multiple_Sheets_Row_Wise()
Dim Work_Sheets() As String
ReDim Work_Sheets(Sheets.Count)
For i = 0 To Sheets.Count - 1
Work_Sheets(i) = Sheets(i + 1).Name
Next i
Sheets.Add.Name = "Combined Sheet"
Dim Row_Index As Integer
Row_Index = Worksheets(1).UsedRange.Cells(1, 1).Row
Dim Column_Index As Integer
Column_Index = 0
For i = 0 To Sheets.Count - 2
Set Rng = Worksheets(Work_Sheets(i)).UsedRange
Rng.Copy
Worksheets("Combined Sheet").Cells(Row_Index, Column_Index + 1).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
Column_Index = Column_Index + Rng.Columns.Count + 1
Next i
Application.CutCopyMode = False
End Sub
⧭ Output:
Run this Macro (Merge_Multiple_Sheets_Row_Wise) and you’ll find the data sets from all the worksheets merged into a single worksheet called Combined_Sheet row-wise.
⧭ Notes:
The name of the merged worksheet is “Combined Sheet”. If you want to name it anything else, name it in the 7th and 15th lines of the code:
Sheets.Add.Name = “Combined Sheet”
We’ve put a 1 column gap between each of the data sets in the combined sheet. If you want to change it, change it in the 16th line of the code:
Column_Index = Column_Index + Rng.Columns.Count + 1
Method 2 – Merge Data Sets from Multiple Sheets Into One Sheet with VBA Column-wise
⧭ VBA Code:
Sub Merge_Multiple_Sheets_Column_Wise()
Dim Work_Sheets() As String
ReDim Work_Sheets(Sheets.Count)
For i = 0 To Sheets.Count - 1
Work_Sheets(i) = Sheets(i + 1).Name
Next i
Sheets.Add.Name = "Combined Sheet"
Dim Column_Index As Integer
Column_Index = Worksheets(1).UsedRange.Cells(1, 1).Column
Dim Row_Index As Integer
Row_Index = 0
For i = 0 To Sheets.Count - 2
Set Rng = Worksheets(Work_Sheets(i)).UsedRange
Rng.Copy
Worksheets("Combined Sheet").Cells(Row_Index + 1, Column_Index).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
Row_Index = Row_Index + Rng.Rows.Count + 1
Next i
Application.CutCopyMode = False
End Sub
⧭ Output:
Run this Macro (Merge_Multiple_Sheets_Column_Wise) and you’ll find the data sets from all the worksheets merged into a single worksheet called Combined_Sheet column-wise.
⧭ Notes:
The name of the merged worksheet is “Combined Sheet”. If you want to name it anything else, name it in the 7th line and 15th line of the code:
Sheets.Add.Name = “Combined Sheet”
We’ve put a 1-row gap between each of the data sets in the combined sheet. If you want to change it, change it in the 16th line of the code:
Row_Index = Row_Index + Rng.Rows.Count + 1
Things to Remember
Each time you run any of these codes, Excel opens a new worksheet called “Combined Sheet” for you in the active workbook. If you already have a worksheet of the same name in your active workbook, rename it or delete it before running the code. Otherwise, you’ll get an error and the code won’t run.
Download the Practice Workbook
<< Go Back To Merge Sheets in Excel | Merge in Excel | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!
With the merge column wise, is there a way you can do this without including the headers from the addition sheets? Just the header from the first sheet so it is just one large table and not separate tables?
Hi, sbellmore
This happens because of the .UsedRange method in the 13th line in the code.
Set Rng = Worksheets(Work_Sheets(i)).UsedRange
You can replace .UsedRange with .Range method to put one dataset or a particular portion of the dataset (eg without headers). For example, with our dataset, you can use the following.
Set Rng = Worksheets(Work_Sheets(i)).Range(“B4:C7”)
or,
Set Rng = Worksheets(Work_Sheets(i)).Range(“B4”).End(xlDown).End(xlToRight)
The downside of using such lines of code is, if you have any dataset without headers, it may ignore the part or whole dataset in some cases. As some of your sheets may not contain a dataset starting from cell B7. For automation, unfortunately, using .UsedRange is the most efficient method.
Hello,
Is there a way to exclude some tabs? I have 10 tabs active but only want to merge 8 of them
Hi, DIEGO.
Thank you for your concern. Yes, there is a way to exclude some tabs and merge only the tabs that you want. The generic code for this is:
Sub Merge_Multiple_Sheets()
Row_Or_Column = Int(InputBox(“Enter 1 to Merge the Sheets Row-wise.” + vbNewLine + vbNewLine + “OR” + vbNewLine + vbNewLine + “Enter 2 to Merge the Sheets Column-wise.”))
Merged_Sheets = InputBox(“Enter the Names of the Worksheets that You Want to Merge. Separate them by Commas.”)
Merged_Sheets = Split(Merged_Sheets, “,”)
Sheets.Add.Name = “Combined Sheet”
Dim Row_Index As Integer
Dim Column_Index As Integer
If Row_Or_Column = 1 Then
Column_Index = Worksheets(1).UsedRange.Cells(1, 1).Column
Row_Index = 0
For i = LBound(Merged_Sheets) To UBound(Merged_Sheets)
Set Rng = Worksheets(Merged_Sheets(i)).UsedRange
Rng.Copy
Worksheets(“Combined Sheet”).Cells(Row_Index + 1, Column_Index).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
Row_Index = Row_Index + Rng.Rows.Count + 1 – 1
Next i
Application.CutCopyMode = False
ElseIf Row_Or_Column = 2 Then
Row_Index = Worksheets(1).UsedRange.Cells(1, 1).Row
Column_Index = 0
For i = LBound(Merged_Sheets) To UBound(Merged_Sheets)
Set Rng = Worksheets(Merged_Sheets(i)).UsedRange
Rng.Copy
Worksheets(“Combined Sheet”).Cells(Row_Index, Column_Index + 1).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
Column_Index = Column_Index + Rng.Columns.Count + 1
Next i
Application.CutCopyMode = False
End If
End Sub
When you’ll run the code, it’ll ask for two inputs. Enter 1 if you want to merge the sheets row-wise, or 2 if you want to merge them column-wise.
And in the second input box, enter the name of the sheets that you want to merge. Don’t forget to put commas between the names, and don’t put any space after the commas.
Once you are done entering the inputs, click OK. You’ll find your selected tabs merged row-wise or column-wise in a new sheet called “Combined Sheet”.
Good luck.
Hello. Is there any reason it only merge data until the 4th sheet? I’ve been running it just fine until i reach the 5th sheet.
Hello ABC,
Thanks for your comment. No, there is no such limit to merging data. This code should perfectly work for the 5th sheet too. However, there are a limit for row (1,048,576) & column (16,384) numbers in Excel. If after merging the 5th sheet your data crosses this limit, it may not work.
Hope you have found your solution. If you face any further problems, please share your Excel file with us at [email protected].
Regards
Arin Islam,
Exceldemy.
hi, if i run the code it is giving correct output only but instead of adding it is rows they were adding in coloums so in which line of code i need to change for coloumn to row?
Hi is there a way to add the sheets in row wise not in coloumn wise for that where i need to change the code?
Hello LAKSHMI,
I hope you are doing well. Thank you for your query. You can combine sheets row-wise using the VBA code provided in the first method of this article. (Merge Data Sets from Multiple Sheets into One Sheet with VBA Row-wise). Once you go through this method of merging data sets, you will know where to change the code as the details are written in the “Note”.
Best Regards,
Afrina Nafisa
Exceldemy
is there a way to merge the data and arrange at the same time under one headline?
Hello Muhammad Farg,
Yes, there are multiple ways to merge data under same headlines. In this article we showed how to merge sheets step by step: How to Merge Sheets in Excel
You can follow this VBA code to merge the data and arrange it at the same time under one headline:
Before using the code make sure both of your sheets contains the same headings.
Sheet1 & Sheet2:
Output:
Excel File: Merge Data in Same Headings.xlsx
Regards
ExcelDemy
this code is not working as a excel add-in. Only when the whole code is written in particular workbook it is working
Hello Mill,
The VBA code is tailored to operate within a specific workbook context and may not be compatible as a stand-alone Excel Add-in without some adjustments. Add-ins typically work across multiple workbooks, so you need to use the modified code to ensure it can handle different active workbooks, rather than just ThisWorkbook.
To make this VBA code work as an add-in, follow the steps given below:
1. Replace references to ThisWorkbook with ActiveWorkbook.
2. Create a new module within the VBA editor and place your code there. This is necessary for turning it into an add-in.
3. Once the code is ready, save the workbook as an Excel Add-in (.xlam) file..
4. Install and test the add-in to verify that it works across different workbooks.
Now, this you can use this code as an add-in. If any further issues arise, you can refine the code further depending on their specific needs.
Regards
ExcelDemy
My workbook has 11 sheets. They are copies of Sheet1 in which one column is changed in each worksheet. So, they are named Sheet1(2), Sheet1(3), and all the way to Sheet1(11).
When I use the code for combining by rows I get a subscript out of range error. It errors out on Set Rng = Worksheets(Merged_Sheets(i)).UsedRange. Also, it adds in 10 blank sheets. What do I need change for the error to go away and for it to not insert 10 blank worksheets?
Hello JaneM,
“Subscript Out of Range Error” occurs when trying to reference a worksheet that does not exist. In your case, ensure that the names of the sheets you are trying to access match exactly with what is in the workbook. To solve this issue you can use the following VBA code.
The code will dynamically checks for the existence of the sheets Sheet1(2) to Sheet1(11) using a loop. It will copy the entire rows based on the last used row in each sheet instead of using UsedRange, which may cause issues if you have formatting or other non-data cells.
The code creates a single new worksheet named MergedData and does not insert any blank sheets.
Copy-Paste the VBA code:
Regards
ExcelDemy
Hi, how can I fix “run time error “6” overflow
I have to mention that i have more than 1 mln rows in 4 columns
Hello Bleona,
You can fix the runtime error 6 overflow by handling the large dataset more efficiently. Since you have over 1 million rows, Excel may be exceeding its memory limits. You can follow these steps:
1. Use smaller data batches for processing.
2. Switch from Integer to Long data types in your VBA code, as Integer can only handle values up to 32,767.
3. Consider using Power Query for merging large datasets instead of VBA, as it handles large data better.
Regards
ExcelDemy