VBA & Macro Problems

Sort Multiple Columns with an Excel VBA Macro

Kushani NimanthikaKushani Nimanthika Nov 22, 2026 869 views

Question details

The user needs an Excel VBA macro that sorts a data range by name and then by date.

Sort Multiple Columns with an Excel VBA Macro
Product
Microsoft Excel
Device & OS
not provided
Scenario
Sort a worksheet table first by names in column E and then by dates in column C.
Observed behavior
The existing recorded macro uses multiple sort fields but does not sort the data correctly.
Before you start

Check the table's first and last columns and rows, and confirm whether row 1 contains headers. Make sure the values in column C are actual Excel dates rather than text if you want chronological order.

Solution 1Recommended

Use a Multi-Level Excel VBA Sort

Define the complete table range and add the name and date sort keys in priority order.

1
Open the VBA editor

In Excel, select Developer > Visual Basic. If the Developer tab is hidden, enable it through Excel's ribbon settings.

2
Add the macro

In the VBA editor, select Insert > Module and paste this code: Sub SortByNameAndDate() Dim ws As Worksheet Set ws = ActiveSheet With ws.Sort .SortFields.Clear .SortFields.Add Key:=ws.Range("E2:E114"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal .SortFields.Add Key:=ws.Range("C2:C114"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal .SetRange ws.Range("A1:G114") .Header = xlYes .Orientation = xlTopToBottom .Apply End With End Sub

3
Adjust the ranges

In the module, replace A1:G114 with the complete table range, including its header row. Update E2:E114 and C2:C114 to cover the corresponding data rows, and change xlAscending to xlDescending for either key if needed.

4
Run the macro

Activate the worksheet to sort, then select Developer > Macros, choose SortByNameAndDate, and select Run. Names are sorted first; dates are sorted within each name.

Use a Multi-Level Excel VBA Sort
Keep the table together: Include every related table column in .SetRange so each row's values move together. If the range has no header row, set .Header to xlNo and include the first data row in both sort keys.
Free Microsoft Office alternative

Sort Spreadsheet Data with WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Excel formats, making it easy to move existing workbooks and sort data. Excel VBA compatibility can vary, so test macros separately.

Sort spreadsheet data by multiple columns.Open and edit Microsoft Excel XLSX and XLS workbooks.Use a familiar interface for a smoother transition from Excel.Work with a free, lightweight office suite.
microsoft office alternative - wps office

Frequently Asked Questions

Which column is sorted first in the macro?

Column E is the primary sort key because it is added first under .SortFields. Column C is the secondary key, so dates are sorted within each name.

How do I sort the dates from newest to oldest?

In the .SortFields.Add line for column C, change Order:=xlAscending to Order:=xlDescending.

Why are my dates not sorting chronologically?

Some values in column C may be stored as text. Convert them to recognized Excel dates before running the macro, and check that the C2:C114 key covers all data rows.

How do I keep the macro in my workbook?

In Excel, select File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Use Developer > Macros to run it later.