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
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Receipt | Customer | Amount | |
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 |
Boston
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Receipt | Customer | Amount | |
| 2 | 1043 | Carter Bakery | 1,250.00 | |
| 3 | 1044 | Peters Motors | 980.50 | |
| 4 | 1045 | Dr. Patel Clinic | 412.00 | |
| 5 |
Leeds
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Receipt | Customer | Amount | |
| 2 | 2101 | Hudson Brewery | 2,300.00 | |
| 3 | 2102 | Miller Sports | 760.00 | |
| 4 | ||||
| 5 |
Denver
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Receipt | Customer | Amount | |
| 2 | 3051 | Shaw Printing | 540.00 | |
| 3 | 3052 | Cloud Cafe | 128.40 | |
| 4 | 3053 | Becker Law | 1,900.00 | |
| 5 | 3054 | Jansen Flowers | 86.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
| Part | What it does |
|---|---|
For Each ws In Worksheets | Visits 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).Row | The 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 Total | The 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).ClearContentsremoves 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
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Receipt | Customer | Amount | Branch |
| 2 | 901 | Carter Bakery | 310.00 | Boston |
| 3 | 902 | Hudson Brewery | 1,840.00 | Leeds |
| 4 | 903 | Cloud Cafe | 95.50 | Denver |
| 5 | 904 | Peters Motors | 620.00 | Boston |
| 6 | 905 | Becker Law | 2,210.00 | Denver |
| 7 | 906 | Jansen Flowers | 48.00 | Denver |
| 8 | 907 | Miller Sports | 1,375.00 | Leeds |
| 9 | 908 | Shaw Printing | 530.00 | Denver |
| 10 | 909 | Dr. Patel Clinic | 260.80 | Boston |
| 11 | 910 | Linden Hotel | 990.00 | Leeds |
Boston
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Receipt | Customer | Amount | |
| 2 | 1043 | Carter Bakery | 1,250.00 | |
| 3 | 1044 | Peters Motors | 980.50 | |
| 4 | 1045 | Dr. Patel Clinic | 412.00 | |
| 5 |
Leeds
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Receipt | Customer | Amount | |
| 2 | 2101 | Hudson Brewery | 2,300.00 | |
| 3 | 2102 | Miller Sports | 760.00 | |
| 4 | ||||
| 5 |
Denver
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Receipt | Customer | Amount | |
| 2 | 3051 | Shaw Printing | 540.00 | |
| 3 | 3052 | Cloud Cafe | 128.40 | |
| 4 | 3053 | Becker Law | 1,900.00 | |
| 5 | 3054 | Jansen Flowers | 86.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
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Item | Qty | Revenue | |
| 2 | Copy paper | 40 | 118.00 | |
| 3 | Black toner | 12 | 534.00 | |
| 4 | Ring binder | 25 | 61.25 | |
| 5 |
February
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Item | Qty | Revenue | |
| 2 | Copy paper | 30 | 88.50 | |
| 3 | Sticky notes | 60 | 147.00 | |
| 4 | ||||
| 5 |
March
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Item | Qty | Revenue | |
| 2 | Black toner | 8 | 356.00 | |
| 3 | Copy paper | 50 | 147.50 | |
| 4 | Ballpoint pens | 100 | 39.00 | |
| 5 | Sticky notes | 15 | 36.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.