How to Merge Multiple Sheets into One Sheet with VBA in Excel?

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.

Worksheet to Merge Multiple Sheets into One Sheet with VBA

Sheet2 contains the sales record of Week 2.

Worksheet to Merge Multiple Sheets into One Sheet with VBA

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

VBA Code to Merge Multiple Sheets into One Sheet with VBA

⧭ 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.

Merge Multiple Sheets into One Sheet with VBA 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

Changing VBA Code to Merge Multiple Sheets into One Sheet with VBA


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

VBA Code to Merge Multiple Sheets into One Sheet with VBA

⧭ 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”

Changing VBA Code to Merge Multiple Sheets into One Sheet with VBA

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!
Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

11 Comments
  1. 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.

  2. 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.

  3. 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.

  4. 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?

  5. 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?

    • Reply Lutfor Rahman Shimanto
      Lutfor Rahman Shimanto Feb 22, 2024 at 4:44 PM

      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

  6. 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:

      
      Sub combine_multiple_sheets()
      Dim Row_1, Col_1, Row_last, Column_last As Long
      Dim headers As Range
      Set WB = ThisWorkbook
      Set wX = WB.Sheets.Add
      wX.Name = "Consolidated"
      Set headers = Application.InputBox("Choose the Headers", Type:=8)
      headers.Copy wX.Range("A1")
      Row_1 = headers.Row + 1
      Col_1 = headers.Column
      For Each Ws In WB.Worksheets
          If Ws.Name <> "Consolidated" Then
              Ws.Activate
              Row_last = Cells(Rows.Count, Col_1).End(xlUp).Row
              Column_last = Cells(Row_1, Columns.Count).End(xlToLeft).Column
              Range(Cells(Row_1, Col_1), Cells(Row_last, Column_last)).Copy wX.Range("A" & wX.Cells(Rows.Count, 1).End(xlUp).Row + 1)
          End If
      Next Ws
      Worksheets("Consolidated").Activate
      End Sub
      

      Output:

      Excel File: Merge Data in Same Headings.xlsx

      Regards
      ExcelDemy

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo