Sort Multiple Columns with an Excel VBA Macro
Question details
The user needs an Excel VBA macro that sorts a data range by name and then by date.

- 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.
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.
Use a Multi-Level Excel VBA Sort
Define the complete table range and add the name and date sort keys in priority order.
In Excel, select Developer > Visual Basic. If the Developer tab is hidden, enable it through Excel's ribbon settings.
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
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.
Activate the worksheet to sort, then select Developer > Macros, choose SortByNameAndDate, and select Run. Names are sorted first; dates are sorted within each name.

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.

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.




