Excel VBA: combine multiple sheets into one

Every branch sends its own sheet, every month sits on its own tab, and the analysis needs it all in one list. A macro stacks the rows of every sheet, keeps the header just once and records where each row came from.

In short: For Each ws In Worksheets visits every sheet, and you skip the summary sheet itself. For each sheet, End(xlUp) finds the last data row there and the first empty row on the summary sheet, then you copy the block from row 2 down: ws.Range("A2:C" & lastRow).Copy Destination:=total.Range("A" & nextRow).

What you need to know: working with worksheets, finding the last row and copy and paste. This lesson puts the three together.

Try it right now

The workbook has four sheets: Total with the header, and three branches with their receipts. Use the tabs above the grid to switch between them. Copy the rows of every branch onto Total.

Task Each of the three branches sends its receipts on its own sheet. The macro CombineSheets should copy the data rows (without the header) of all branch sheets onto the Total sheet, one block below the other, starting in row 2 and in sheet order. Total already has the header. Check tests it with a different number of rows per branch.

Total

ABCD
1ReceiptCustomerAmount
2
3
4
5

Boston

ABCD
1ReceiptCustomerAmount
21043Carter Bakery1,250.00
31044Peters Motors980.50
41045Dr. Patel Clinic412.00
5

Leeds

ABCD
1ReceiptCustomerAmount
22101Hudson Brewery2,300.00
32102Miller Sports760.00
4
5

Denver

ABCD
1ReceiptCustomerAmount
23051Shaw Printing540.00
33052Cloud Cafe128.40
43053Becker Law1,900.00
53054Jansen Flowers86.00

How combining works

Sub CombineSheets()
    Dim ws As Worksheet
    Dim lastRow As Long, nextRow As Long
    For Each ws In Worksheets
        If ws.Name <> "Total" Then
            lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
            nextRow = Worksheets("Total").Cells(Rows.Count, "A").End(xlUp).Row + 1
            ws.Range("A2:C" & lastRow).Copy Destination:=Worksheets("Total").Range("A" & nextRow)
        End If
    Next ws
End Sub
PartWhat it does
For Each ws In WorksheetsVisits every worksheet in the workbook, left to right. A new branch sheet is picked up without changing the code.
If ws.Name <> "Total"Skips the summary sheet, otherwise the macro would copy Total onto itself.
ws.Cells(ws.Rows.Count, "A").End(xlUp).RowThe last used row on the branch sheet. The ws. in front of Cells matters; without it the active sheet is used.
… End(xlUp).Row + 1 on TotalThe first empty row on the summary sheet. It is looked up again for every sheet because it moves down after each block.
Range("A2:C" & lastRow)The data block without its header, so Total ends up with one header only.
Copy Destination:=…Copies values and formats in one go, without the clipboard and without Select.

After the run, Total lists Boston first, then Leeds, then Denver: the order of the tabs decides the order of the blocks. If you want values without formats, assign the value of a range of the same size instead of using Copy: total.Range("A" & nextRow).Resize(lastRow - 1, 3).Value = ws.Range("A2:C" & lastRow).Value.

Where did this row come from? Add the sheet name

Once everything sits on one sheet, you can no longer tell which branch a row belongs to. So you write the sheet name into a column of its own. After copying you know how many rows were added (lastRow - 1), and exactly that many cells get the name:

total.Range("D" & nextRow).Resize(lastRow - 1, 1).Value = ws.Name

Assigning a single value to a multi-cell range puts it in every cell of the range. No loop needed.

Clear the summary sheet or create a new one

A combine macro usually runs again every month. If Total still holds the old data, the macro either appends the new rows below it, or leaves old rows at the bottom when there are fewer receipts this time. Two clean ways out:

  • Clear it: total.Range("A2:D" & total.Rows.Count).ClearContents removes everything below the header. Header, column widths and formats stay.
  • Create it: Set total = Worksheets.Add(After:=Worksheets(Worksheets.Count)) adds an empty sheet at the end, total.Name = "Total" names it. Then take the header once from the first sheet.

Clearing suits a summary sheet that belongs to the workbook for good, for example because formulas or a chart point at it. Creating suits a result that should be built fresh each time. In that case the sheet must not exist yet, because Excel does not allow two sheets with the same name.

Another exercise: clear first, add the branch

Total still has last month on it. Tidy up first, then collect, and put the branch in column D.

Task Total still holds last month's receipts. The macro Collect should first clear all old rows below the header, then copy the data rows of every branch sheet onto Total one block below the other (starting in row 2), and write the name of the source sheet into column D for every row.

Total

ABCD
1ReceiptCustomerAmountBranch
2901Carter Bakery310.00Boston
3902Hudson Brewery1,840.00Leeds
4903Cloud Cafe95.50Denver
5904Peters Motors620.00Boston
6905Becker Law2,210.00Denver
7906Jansen Flowers48.00Denver
8907Miller Sports1,375.00Leeds
9908Shaw Printing530.00Denver
10909Dr. Patel Clinic260.80Boston
11910Linden Hotel990.00Leeds

Boston

ABCD
1ReceiptCustomerAmount
21043Carter Bakery1,250.00
31044Peters Motors980.50
41045Dr. Patel Clinic412.00
5

Leeds

ABCD
1ReceiptCustomerAmount
22101Hudson Brewery2,300.00
32102Miller Sports760.00
4
5

Denver

ABCD
1ReceiptCustomerAmount
23051Shaw Printing540.00
33052Cloud Cafe128.40
43053Becker Law1,900.00
53054Jansen Flowers86.00

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

One more: a new summary sheet and an empty month

This time there is no summary sheet; the macro creates it. Check also runs it on a workbook where one month has nothing but its header.

Task For the quarter, the macro Quarter should add a new sheet Total after the last sheet, copy the header once from the first sheet and then copy the data rows of all month sheets below it, one block after the other. Watch out: a month can be empty, with only the header on it.

January

ABCD
1ItemQtyRevenue
2Copy paper40118.00
3Black toner12534.00
4Ring binder2561.25
5

February

ABCD
1ItemQtyRevenue
2Copy paper3088.50
3Sticky notes60147.00
4
5

March

ABCD
1ItemQtyRevenue
2Black toner8356.00
3Copy paper50147.50
4Ballpoint pens10039.00
5Sticky notes1536.75

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

In real Excel

With many sheets, put Application.ScreenUpdating = False at the start and = True at the end: the screen stops flickering and the macro runs faster (see With and ScreenUpdating). Try a combine macro on a copy of the workbook first, because ClearContents and copies cannot be undone with Ctrl+Z. And watch out for sheets that should not be included, such as a notes page or a sheet with master data. Leave them out in the If, just like the summary sheet.

Common mistakes

Not skipping the summary sheet

Without If ws.Name <> "Total", Total is the first sheet in the loop. Its last row is the header, so the macro copies the header into row 2. Check then tells you that Total!A2 contains Receipt where it expected 1043.

Cells without a sheet

lastRow = Cells(Rows.Count, "A").End(xlUp).Row without ws. looks at the active sheet, which is Total, no matter which sheet the loop is on. You get the same stray header in row 2. Inside the loop, every Cells and Range needs the sheet it refers to.

An empty sheet copies its header

If a sheet only has its header, lastRow is 1. To Excel, Range("A2:C1") is the same range as A1:C2, so the header lands in the middle of the data. Fix: wrap the copy in If lastRow > 1 Then.

The new sheet ends up in the wrong place

Worksheets.Add with no arguments inserts the sheet before the active sheet, not at the end. The third exercise then says "The sheets are not right. Expected: January, February, March, Total. Yours: Total, January, February, March." With After:=Worksheets(Worksheets.Count) it goes to the end.

The sheet already exists

Run a macro with Worksheets.Add a second time and total.Name = "Total" fails because the name is taken. Excel and the exercises report "Run-time error '1004': That name is already taken. Try a different one." Check whether the sheet is there first, then delete or clear it. The lesson enable macros and code safely shows how.

FAQ

What about Excel's Consolidate feature?

Consolidate on the Data tab combines ranges with the same layout and calculates while doing so, for example a sum per category. It does not stack rows. For a complete list of every receipt you need a macro like this one, or Power Query.

Do all sheets need the same layout?

Yes, otherwise the columns on the summary sheet do not line up. If one branch has the amount in column D instead of C, it ends up in the wrong column. Compare the headers before combining, for example against the header of the first sheet.

Can I combine sheets from other files?

In real Excel, yes: the macro opens each file with Workbooks.Open and copies its sheets. The exercises here have no file system, so all sheets live in one workbook. The loop and the copy step are the same.

Why not loop with For i = 1 To Worksheets.Count?

That works just as well; Worksheets(i) is then the i-th sheet. For Each is shorter and reads like the task. If you need the position, for example to take only sheets left of the summary sheet, use ws.Index.

Up next: create a report

Your data is now in one list. Next, a macro turns it into a finished report: a new sheet, a title, totals per group, number formats and, if you like, one sheet per category.

Create a report

Write to us

Choose language

Select your preferred language