VBA & Macro Problems

Split Excel Records into Worksheets by Dispatcher and Date with VBA

Anum MushtaqAnum Mushtaq Nov 25, 2026 870 views

Question details

The user wants to split records into separate worksheets by dispatcher, including only records from a specified date onward.

Split Excel Records into Worksheets by Dispatcher and Date with VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A MAIN worksheet contains dispatcher names and dated records that need to be distributed to individual worksheets.
Observed behavior
Each dispatcher worksheet should contain the matching records from the chosen date onward, with values, formatting, and column widths copied from the source.
Before you start

In the workbook, confirm that MAIN has headers in row 1, dispatcher names in column B, and valid Excel dates in column C. The macro below is for desktop Excel on Windows; change DATE_COL in the code if your dates are in another column.

Solution 1Recommended

Run a VBA macro to create a worksheet for each dispatcher

Use this method to create dispatcher worksheets with records dated on or after a date you enter.

The macro creates a sheet for every nonblank dispatcher name, even if that dispatcher has no records on or after the chosen date. It copies values rather than formulas and preserves cell formatting and column widths.

Existing sheets are not deleted or replaced. If a sheet name is already in use, the macro adds a numeric suffix. Dispatcher names are grouped without regard to letter case.

1
Prepare the workbook

Open the workbook in desktop Excel on Windows. Select File > Save As and save it as an Excel Macro-Enabled Workbook (.xlsm). Make sure the source worksheet is named MAIN.

2
Open the VBA editor

Press Alt+F11, select your workbook in the Project pane, and choose Insert > Module. Paste the following code into the module: Option Explicit Sub SplitByDispatcherAndDate() Const DATE_COL As Long = 3 ' Column C; change if needed Dim wb As Workbook, src As Worksheet, dest As Worksheet Dim groups As Object Dim lastRow As Long, lastCol As Long, r As Long, c As Long, nextRow As Long Dim dispatcher As String, entered As String, cutoff As Date Dim sourceRange As Range, destRange As Range Set wb = ThisWorkbook On Error Resume Next Set src = wb.Worksheets("MAIN") On Error GoTo 0 If src Is Nothing Then MsgBox "The MAIN worksheet was not found." Exit Sub End If entered = InputBox("Enter the starting date using your system's date format:") If entered = "" Then Exit Sub If Not IsDate(entered) Then MsgBox "Enter a valid date." Exit Sub End If cutoff = DateValue(entered) Set groups = CreateObject("Scripting.Dictionary") groups.CompareMode = vbTextCompare lastRow = src.Cells(src.Rows.Count, 2).End(xlUp).Row lastCol = src.Cells(1, src.Columns.Count).End(xlToLeft).Column If lastCol < DATE_COL Then lastCol = DATE_COL If lastCol < 2 Then lastCol = 2 For r = 2 To lastRow If Not IsError(src.Cells(r, 2).Value) Then dispatcher = Trim(CStr(src.Cells(r, 2).Value)) If dispatcher <> "" Then If Not groups.Exists(dispatcher) Then Set dest = wb.Worksheets.Add(After:=wb.Worksheets(wb.Worksheets.Count)) dest.Name = AvailableSheetName(wb, dispatcher) groups.Add dispatcher, dest Set sourceRange = src.Cells(1, 1).Resize(1, lastCol) Set destRange = dest.Cells(1, 1).Resize(1, lastCol) sourceRange.Copy destRange.PasteSpecial xlPasteFormats destRange.Value2 = sourceRange.Value2 For c = 1 To lastCol dest.Columns(c).ColumnWidth = src.Columns(c).ColumnWidth Next c End If If Not IsError(src.Cells(r, DATE_COL).Value) Then If IsDate(src.Cells(r, DATE_COL).Value) Then If DateValue(src.Cells(r, DATE_COL).Value) >= cutoff Then Set dest = groups.Item(dispatcher) nextRow = dest.Cells(dest.Rows.Count, 2).End(xlUp).Row + 1 Set sourceRange = src.Cells(r, 1).Resize(1, lastCol) Set destRange = dest.Cells(nextRow, 1).Resize(1, lastCol) sourceRange.Copy destRange.PasteSpecial xlPasteFormats destRange.Value2 = sourceRange.Value2 End If End If End If End If End If Next r Application.CutCopyMode = False MsgBox "Dispatcher worksheets have been created." End Sub Private Function AvailableSheetName(wb As Workbook, dispatcher As String) As String Dim baseName As String, candidate As String, suffix As String Dim ch As String, i As Long, n As Long Dim existing As Worksheet baseName = "Disp_" For i = 1 To Len(dispatcher) ch = Mid(dispatcher, i, 1) If AscW(ch) < 32 Or InStr(1, ":\/?*[]'", ch, vbBinaryCompare) > 0 Then ch = "_" baseName = baseName & ch Next i baseName = Left(baseName, 31) candidate = baseName n = 1 Do Set existing = Nothing On Error Resume Next Set existing = wb.Worksheets(candidate) On Error GoTo 0 If existing Is Nothing Then Exit Do n = n + 1 suffix = "_" & CStr(n) candidate = Left(baseName, 31 - Len(suffix)) & suffix Loop AvailableSheetName = candidate End Function

3
Run the macro

Return to Excel, press Alt+F8, select SplitByDispatcherAndDate, and click Run. Enter the starting date in your system's date format.

4
Check the results

Open the new Disp_ worksheets and verify the dispatcher names and dates. Rows with blank dispatcher names, invalid dates, or dates before the cutoff are not copied as records.

Run a VBA macro to create a worksheet for each dispatcher
Adjust the source columns: The macro uses column B to find dispatcher names and the last source row. It copies columns A through the last populated header column, or at least through the date column. Make sure every column you need has a header in row 1.
Free Microsoft Office alternative

Work with dispatcher spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for opening and editing compatible Excel files. You can migrate existing spreadsheets without rebuilding them, but VBA support varies by edition and platform, so test macros before relying on them.

Review and edit dispatcher records in a familiar spreadsheet interface.Open and edit compatible Microsoft Excel formats, including XLSX files.Move existing spreadsheets into a lightweight Office alternative.Use free spreadsheet tools for everyday data review.
microsoft office alternative - wps office

Frequently Asked Questions

How do I change the date column used by the macro?

In the VBA editor, find Const DATE_COL As Long = 3 and change 3 to the column number containing dates. For example, use 4 for column D.

Does the macro delete existing dispatcher worksheets?

No. It creates new worksheets and adds a numeric suffix when a generated name is already in use. Running it again creates another set of sheets.

Are records on the date I enter included?

Yes. The macro includes valid dates equal to or later than the entered date. It compares dates without requiring the times to match.

Why are some records missing from the new worksheets?

Check column B for blank or error values and column C, or your configured date column, for invalid dates or dates before the cutoff. Also check that the source sheet is named MAIN.

Does the macro copy formulas?

No. It copies the displayed underlying values and cell formatting, plus column widths. Formulas in the source are not retained as formulas in the new sheets.