EXCEL AUTOMATION & VBA

Split Data into Multiple Sheets with VBA (One Sheet per Value)

You have one long list (orders, employees, transactions) and you need a separate sheet for each region, rep, customer or month. Filtering and copying by hand takes minutes and goes wrong when you repeat it. This free macro splits any table into one sheet per value in a column, in one click, replaces the old sheets when you run it again, and writes a log that proves no rows were lost.

Excel VBA macro that split 40 orders into one sheet per region, with West 12 rows, East 11 and North 10, and a check that ties.
40 rows, 4 sheets, totals tie.

Quick answer

  1. Save your workbook as .xlsm (macro-enabled).
  2. Press Alt+F11, choose Insert > Module and paste the code below (or File > Import File for the .txt/.bas file).
  3. Change the four settings at the top: source sheet, column to split by, header row, column to total.
  4. Press Alt+F8, choose SplitDataToSheets and click Run.
Free download: Get the sample data (.xlsx) and the macro (split-data-to-sheets.txt). The sample has 40 orders in 4 regions so you can test the macro before you run it on your own data. Works in Excel 2010 and later for Windows and Mac (Microsoft 365 desktop). VBA does not run in Excel for the web.
Before you run it: Save a copy of your file first. Macros cannot be undone with Ctrl+Z. The macro only deletes sheets that it created itself (one per value plus the Split Log), but if you already have a sheet called, for example, West, it will be replaced.

Step 1: Check the data

The macro expects a normal table: one header row, one row per record, no blank rows in the middle. The column you split by can contain anything (text, numbers, dates). In the sample, column C is the Region. One order was typed as "west " with a trailing space, which happens with data exported from other systems. The macro trims and ignores case, so that order still goes to the West sheet. If you are not sure your values are clean, see remove extra spaces and hidden characters.

Excel order data with 40 rows and a Region column of North, South, East and West, including one region typed as west with a trailing space in row 19.
Region values, one of them typed differently.

Step 2: Paste the macro and change the settings

Press Alt+F11 to open the VBA editor, select your workbook in the Project pane, choose Insert > Module and paste the full code:

Option Explicit

' OneXcel Studio | Split data into one sheet per value
' Copies every row of the source sheet to a sheet named after the value in KEY_COL.
' Re-running replaces the sheets it created before. Adds a Split Log with a check.

Private Const SRC_SHEET As String = "Data"   ' sheet with the data
Private Const KEY_COL As Long = 3             ' column to split by (3 = C, Region)
Private Const HDR_ROW As Long = 1             ' header row
Private Const SUM_COL As Long = 6             ' column to total in the log (6 = F)

Public Sub SplitDataToSheets()
    Dim wb As Workbook, src As Worksheet, out As Worksheet, logWs As Worksheet
    Dim lastRow As Long, lastCol As Long, r As Long, c As Long, n As Long, i As Long
    Dim keys As New Collection, sheetNames As New Collection
    Dim k As String, nm As String
    Dim data As Variant, buf() As Variant
    Dim total As Double, srcTotal As Double, amtCol As Long, keyRows As Long

    Set wb = ThisWorkbook
    Set src = wb.Worksheets(SRC_SHEET)
    lastRow = src.Cells(src.Rows.Count, KEY_COL).End(xlUp).Row
    lastCol = src.Cells(HDR_ROW, src.Columns.Count).End(xlToLeft).Column
    If lastRow <= HDR_ROW Then MsgBox "No data below the header row.": Exit Sub
    ' Read everything once; Value2 returns dates as serial numbers (fast, no conversion)
    data = src.Range(src.Cells(HDR_ROW, 1), src.Cells(lastRow, lastCol)).Value2
    amtCol = SUM_COL

    ' 1. Unique values (TRIM, ignore case); the first spelling becomes the sheet name
    On Error Resume Next
    For r = 2 To UBound(data, 1)
        k = UCase$(Trim$(CStr(data(r, KEY_COL))))
        If k <> "" Then
            keys.Add k, k
            If Err.Number = 0 Then sheetNames.Add SafeName(Trim$(CStr(data(r, KEY_COL)))), k
            Err.Clear
        End If
    Next r
    On Error GoTo 0

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    ' 2. Delete sheets from an earlier run
    On Error Resume Next
    wb.Worksheets("Split Log").Delete
    For i = 1 To keys.Count
        wb.Worksheets(sheetNames(keys(i))).Delete
    Next i
    On Error GoTo 0
    Application.DisplayAlerts = True

    Set logWs = wb.Worksheets.Add(After:=wb.Worksheets(wb.Worksheets.Count))
    logWs.Name = "Split Log"
    logWs.Range("A1:C1").Value = Array("Sheet", "Rows", "Total")

    ' 3. One sheet per value: write the matching rows in one block
    For i = 1 To keys.Count
        k = keys(i): nm = sheetNames(k)
        ReDim buf(1 To UBound(data, 1), 1 To lastCol)
        For c = 1 To lastCol: buf(1, c) = data(1, c): Next c
        n = 1: total = 0
        For r = 2 To UBound(data, 1)
            If UCase$(Trim$(CStr(data(r, KEY_COL)))) = k Then
                n = n + 1
                For c = 1 To lastCol: buf(n, c) = data(r, c): Next c
                buf(n, KEY_COL) = nm
                If IsNumeric(data(r, amtCol)) Then total = total + data(r, amtCol)
            End If
        Next r
        Set out = wb.Worksheets.Add(After:=wb.Worksheets(wb.Worksheets.Count))
        out.Name = nm
        out.Range("A1").Resize(n, lastCol).Value = buf
        For c = 1 To lastCol                         ' keep the source number formats
            out.Columns(c).NumberFormat = src.Cells(HDR_ROW + 1, c).NumberFormat
        Next c
        out.Rows(1).Font.Bold = True
        out.Columns.AutoFit
        logWs.Cells(i + 1, 1).Value = nm
        logWs.Cells(i + 1, 2).Value = n - 1
        logWs.Cells(i + 1, 3).Value = total
    Next i

    ' 4. Check: rows and totals add back to the source
    For r = 2 To UBound(data, 1)
        If Trim$(CStr(data(r, KEY_COL))) <> "" Then keyRows = keyRows + 1
        If Trim$(CStr(data(r, KEY_COL))) <> "" And IsNumeric(data(r, amtCol)) Then _
            srcTotal = srcTotal + data(r, amtCol)
    Next r
    n = keys.Count + 2
    logWs.Cells(n, 1).Value = "Total"
    logWs.Cells(n, 2).Formula = "=SUM(B2:B" & n - 1 & ")"
    logWs.Cells(n, 3).Formula = "=SUM(C2:C" & n - 1 & ")"
    logWs.Cells(n + 1, 1).Value = "Source rows / total"
    logWs.Cells(n + 1, 2).Value = keyRows
    logWs.Cells(n + 1, 3).Value = srcTotal
    logWs.Cells(n + 2, 1).Value = "Check"
    logWs.Cells(n + 2, 2).Formula = "=IF(AND(B" & n & "=B" & n + 1 & _
        ",ROUND(C" & n & "-C" & n + 1 & ",2)=0),""Ties"",""Check"")"
    logWs.Columns("C").NumberFormat = "#,##0.00"
    logWs.Rows(1).Font.Bold = True
    logWs.Columns("A:C").AutoFit
    src.Activate
    Application.ScreenUpdating = True
    MsgBox keys.Count & " sheets created from " & keyRows & " rows.", vbInformation
End Sub

' Excel sheet names: max 31 characters (kept to 28 here), none of : \ / ? * [ ]
Private Function SafeName(ByVal s As String) As String
    Dim ch As Variant
    For Each ch In Array(":", "\", "/", "?", "*", "[", "]")
        s = Replace(s, ch, "-")
    Next ch
    If s = "" Then s = "Blank"
    s = Left$(s, 28)
    ' never reuse the name of the source sheet or the log
    If StrComp(s, SRC_SHEET, vbTextCompare) = 0 Or _
       StrComp(s, "Split Log", vbTextCompare) = 0 Then s = s & " 2"
    SafeName = s
End Function

Only the four constants at the top need changing for your data:

SettingSample valueMeaning
SRC_SHEET"Data"Name of the sheet with the data
KEY_COL3Column to split by (A = 1, B = 2, C = 3…)
HDR_ROW1Row with the headers
SUM_COL6Column to total in the Split Log (Amount, column F)
VBA editor showing the four settings at the top of the macro: the source sheet Data, the column to split by 3, the header row 1 and the column to total 6.
Four settings, nothing else to edit.

Step 3: How it finds the unique values

    ' 1. Unique values (TRIM, ignore case); the first spelling becomes the sheet name
    On Error Resume Next
    For r = 2 To UBound(data, 1)
        k = UCase$(Trim$(CStr(data(r, KEY_COL))))
        If k <> "" Then
            keys.Add k, k
            If Err.Number = 0 Then sheetNames.Add SafeName(Trim$(CStr(data(r, KEY_COL)))), k
            Err.Clear
        End If
    Next r
    On Error GoTo 0

The macro reads the whole table into an array in one step (much faster than reading cell by cell). It then adds each cleaned value to a Collection using the value as the key. A Collection cannot hold the same key twice, so the second North raises an error that On Error Resume Next skips: what is left is the list of unique values. The first spelling of each value becomes the sheet name, cleaned of characters Excel does not allow in sheet names (: \ / ? * [ ]) and cut to 28 characters.

VBA code that reads the data into an array and collects the unique Region values in a Collection, ignoring case and extra spaces.
A Collection keeps one of each.

Step 4: How it writes each sheet

For each value, the macro loops through the rows, copies the matching ones into a second array and writes that array to a new sheet with a single Range.Value assignment. Writing once per sheet instead of copying row by row keeps the macro fast on tens of thousands of rows. It copies values and number formats, then bolds the header and autofits the columns (the same AutoFit used in autofit the used range with VBA). Cell colours and formulas are not copied; the new sheets are a snapshot of the values.

VBA code that copies the matching rows for each value into an array and writes them to a new sheet in one step.
One write per sheet.

Step 5: Run it and check the result

Press Alt+F8, choose SplitDataToSheets and click Run. The sample creates four sheets in the order the regions first appear: North (10 orders), East (11), South (7) and West (12). Each sheet has the header row and only its own rows, with the Region spelled consistently.

The West sheet created by the macro with the header row and 12 West orders, including the order typed as west.
The West sheet, including the ‘west ‘ order.
Try it: Run the macro, then change one Region on Data from North to South (for example C2) and run it again. North drops to 9 rows (9,900.21), South rises to 8 (6,893.16), the old sheets are replaced rather than duplicated and the Split Log still shows Ties.

Step 6: The Split Log proves nothing was lost

The Split Log sheet lists every new sheet with its row count and the total of the SUM_COL column, then compares them with the source data: 40 rows and 43,352.12 on both sides, so the check shows Ties. If the check says Check, a row has a blank value in the split column; those rows are not copied, so fill them in or give them a value like Unassigned. To compare two lists of IDs afterwards, see compare two lists in Excel.

Split Log sheet with rows and totals for North, East, South and West adding up to 40 rows and 43,352.12, matching the source, with the check showing Ties.
Rows and totals tie.

Variations

Troubleshooting

  • Subscript out of range: the SRC_SHEET name does not match your sheet tab exactly.
  • Nothing happens: macros are disabled. Save as .xlsm and enable content, or check File > Options > Trust Center.
  • Dates show as numbers: the first data row of that column has no date format; format it on the source sheet and run again.
  • Run-time error 1004 on out.Name: a value is a reserved name (History) or two values clean to the same 28 characters; rename them in the data.
  • Need to clear the input sheet after splitting? See clear input cells with a macro.

Frequently asked questions

How do I split data into multiple sheets in Excel without VBA?

Use a PivotTable with the column in the Filters area and PivotTable Analyze > Options > Show Report Filter Pages, or FILTER formulas on each sheet in Microsoft 365. The macro is faster when you repeat the split every week.

Does the macro change my original data?

No. It only reads the source sheet. It deletes and recreates the sheets it made in an earlier run and the Split Log.

Can it split into separate workbooks instead of sheets?

Yes, with a few extra lines: after each sheet is created, use out.Copy and save the new workbook with ActiveWorkbook.SaveAs.

How many rows can it handle?

It reads and writes in arrays, so tens of thousands of rows are fine. Very large files are limited by memory rather than speed.

Does it work on a Mac?

Yes, in Excel for Mac 2016 and later. It does not run in Excel for the web, which does not support VBA.

Final checklist

  • File saved as .xlsm, with a backup copy.
  • Four settings match your data.
  • No blank values in the split column.
  • Macro run with Alt+F8.
  • Split Log check shows Ties.

Automate the repetitive parts

Find more ready-to-use Excel templates, learn the formulas in the OneXcel Tutorial Hub, or turn each sheet into a report with the OneXcel Dashboard Builder.