VBA macros · Workbooks and files
VBA macro to combine all sheets into one sheet in Excel
Stack the data from every worksheet of a workbook into one Combined sheet, with the sheet name in the first column. Headings are taken once.
Data split into one sheet per month or per region is easy to read but hard to analyse: pivot tables, filters and formulas want one long list. This macro creates a sheet called Combined, copies the rows of every other worksheet below each other and writes the source sheet name in column A.
Option Explicit
Sub CombineAllSheets()
Const OUT_NAME As String = "Combined"
Dim wb As Workbook
Dim ws As Worksheet, out As Worksheet
Dim lastRow As Long, lastCol As Long, nextRow As Long
Dim headerDone As Boolean
Set wb = ActiveWorkbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
' Start with a fresh Combined sheet
On Error Resume Next
wb.Worksheets(OUT_NAME).Delete
On Error GoTo Fail
Set out = wb.Worksheets.Add(Before:=wb.Sheets(1))
out.Name = OUT_NAME
nextRow = 2
For Each ws In wb.Worksheets
If ws.Name <> OUT_NAME Then
lastRow = LastUsedRow(ws)
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
If Not headerDone And lastRow >= 1 Then
out.Cells(1, 1).Value = "Sheet"
out.Cells(1, 2).Resize(1, lastCol).Value = ws.Cells(1, 1).Resize(1, lastCol).Value
headerDone = True
End If
If lastRow >= 2 Then
out.Cells(nextRow, 2).Resize(lastRow - 1, lastCol).Value = _
ws.Range(ws.Cells(2, 1), ws.Cells(lastRow, lastCol)).Value
out.Cells(nextRow, 1).Resize(lastRow - 1, 1).Value = ws.Name
nextRow = nextRow + lastRow - 1
End If
End If
Next ws
out.Rows(1).Font.Bold = True
out.Columns.AutoFit
Done:
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Exit Sub
Fail:
MsgBox "Stopped: " & Err.Description, vbExclamation
Resume Done
End Sub
Private Function LastUsedRow(ws As Worksheet) As Long
Dim c As Range
Set c = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
If Not c Is Nothing Then LastUsedRow = c.Row
End FunctionHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module. An empty code window opens on the right.
- Paste the whole code into that window. If the module already starts with
Option Explicit, do not paste that line twice. - Go back to Excel, press
Alt+F8(Mac:Opt+F8, or Tools › Macro › Macros), pickCombineAllSheetsand click Run. - Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.
To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.
How it works
An old Combined sheet is deleted first, with alerts off so Excel does not ask for confirmation, and a new one is added at the front. That makes the macro safe to rerun after the source sheets change. The deletion is wrapped in On Error Resume Next because the sheet will not exist on the first run.
For every other worksheet, LastUsedRow finds the last row that contains anything by searching backwards with Find. The number of columns comes from row 1. The headings of the first sheet become the headings of the result, shifted one column to the right to make room for the Sheet column.
Data rows are transferred with a value assignment between two ranges of equal size. That is far faster than copy and paste and copies results, not formulas, so the combined list does not depend on the source sheets. Finally column A of those rows gets the sheet name, and nextRow moves down by the number of rows copied. Empty sheets are skipped because their lastRow is below 2.
Change it
To skip certain sheets, such as a summary or a lookup sheet, extend the If line: If ws.Name <> OUT_NAME And ws.Name <> "Summary" And ws.Visible = xlSheetVisible Then.
If the headings are not in row 1, for example a title in row 1 and headings in row 3, change the header row to 3 and the first data row to 4 in both places. If you need formats and formulas copied as they are, use ws.Range(...).Copy out.Cells(nextRow, 2) instead of the value assignment.
To combine sheets from several workbooks in a folder, see VBA macro to merge all workbooks in a folder into one sheet. Afterwards, turn the result into an Excel table (Ctrl+T) and build a pivot table from it; Excel tables in VBA: working with ListObjects shows how to do that with code as well.
What can go wrong
OUT_NAME, if you want to keep a result.All sheets should have the same columns in the same order; otherwise data lands under the wrong headings. Sheets with more columns than the first sheet are copied in full but those extra columns have no heading. Merged cells and tables in the sources are fine, since only values are copied. A protected workbook structure blocks adding the sheet and the macro reports it.
=VSTACK(Jan:Dec!A2:D100) can stack sheets live, but it includes empty rows and needs a fixed range; the macro adapts to each sheet's size.Related: VBA macro to merge all workbooks in a folder into one sheet, VBA macro to save each sheet as a separate workbook in Excel, VSTACK.