VBA & Macro Problems

Sort an Excel Table by Two Columns with VBA

Olivia MillerOlivia Miller Dec 2, 2026 869 views

Question details

Sort a formatted Excel table first by Supplier Name and then by Product Description using VBA.

Sort an Excel Table by Two Columns with 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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, select Developer > Visual Basic. In the VBA editor, select Insert > Module.

2
Add the sort macro

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

3
Run the macro

Return to Excel and select the worksheet containing the table. Choose Developer > Macros, select SortSupplierTable, and click Run.

Sort the table using named VBA sort keys
Keep the headers and sort order: Header:=xlYes identifies the first row as headers. Supplier Name is the primary key; Product Description sorts rows within each supplier group. Enable macros only if you trust the workbook.
Free Microsoft Office alternative

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.

Sort spreadsheet data by columns using the familiar interfaceCompatible with Microsoft Excel formats, including .xlsxFree and lightweight for everyday spreadsheet workA familiar interface helps make migration from Office straightforward
microsoft office alternative - wps office

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.