Sort an Excel Table by Two Columns with VBA
Question details
Sort a formatted Excel table first by Supplier Name and then by Product Description using VBA.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A user wants a macro to apply a two-column ascending sort to an Excel table.
- Observed behavior
- The desired result is a table grouped in ascending order by supplier, with product descriptions sorted within each supplier group.
Click inside the table and check its name on the Table Design tab, as well as the exact column headers. Use a desktop version of Excel that supports VBA, and save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if you want to keep the macro.
Sort the table using named VBA sort keys
Use the table's column names as sort keys so the macro does not depend on worksheet columns C and D.
In Excel, select Developer > Visual Basic. In the VBA editor, select Insert > Module.
Paste the following code into the module. Replace Table1 with your table name and adjust the two column names if needed: Sub SortSupplierTable() Dim tbl As ListObject Set tbl = ActiveSheet.ListObjects("Table1") tbl.Range.Sort Key1:=tbl.ListColumns("Supplier Name").Range, _ Order1:=xlAscending, _ Key2:=tbl.ListColumns("Product Description").Range, _ Order2:=xlAscending, _ Header:=xlYes, Orientation:=xlTopToBottom End Sub
Return to Excel and select the worksheet containing the table. Choose Developer > Macros, select SortSupplierTable, and click Run.

Work with sorted spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and use WPS Spreadsheets to sort data as you move between office suites; Excel-specific VBA behavior may differ.

Frequently Asked Questions
How do I find my Excel table name?
Click any cell in the table, open the Table Design tab, and look for the Table Name box. Use that name in ActiveSheet.ListObjects("Table1").
Which column is sorted first?
Key1 is the primary sort key, so Supplier Name is sorted first. Key2 sorts Product Description within rows that have the same supplier.
How can I sort one column in descending order?
In the VBA code, change Order1 or Order2 from xlAscending to xlDescending for the corresponding sort key.
Why can't the macro find my table or column?
Check that the worksheet containing the table is active, then verify the table name on Table Design and match the column names in ListColumns to the headers exactly.




