Excel VBA Sort Table Ascending: Step-by-Step Guide
In this article
To sort a table ascending with Excel VBA, use the Sort method on the table's Range, specifying the key column and xlAscending. The exact syntax depends on whether you are sorting a ListObject (a real Excel table) or a plain range. This guide gives you working code for both, plus the mistakes that cause run-time errors and the fixes.
What You'll Need
- Excel for Windows or Mac with macros enabled (the Developer tab visible, or the ability to open the VBA editor with Alt+F11 / Option+F11).
- A worksheet containing either a formatted Excel table (Insert > Table) or a contiguous range with a header row.
- Basic familiarity with the VBA editor: inserting a module and running a macro.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project pane, right-click your workbook, choose Insert > Module. A blank code window appears.
- Identify your table. If your data is a real Excel table, click any cell in it and note the table name on the Table Design tab (the ribbon shows something like Table1). If it is a plain range, note the range address, for example A1:D100.
- Write the ListObject sort macro. Paste this into the module, replacing Table1 and the column name with your own:
This sorts the whole table by the column called Name, keeping rows intact.Sub SortTableAscending() Dim tbl As ListObject Set tbl = ThisWorkbook.Worksheets("Sheet1").ListObjects("Table1") With tbl.Sort .SortFields.Clear .SortFields.Add Key:=tbl.ListColumns("Name").Range, _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal .Header = xlYes .Apply End With End Sub - Write the plain-range version if you do not use a table. Use this pattern, adjusting the range and key column:
The SetRange line must cover the entire block, including the header, or the header will be sorted as data.Sub SortRangeAscending() With ActiveSheet.Sort .SortFields.Clear .SortFields.Add Key:=Range("A2:A100"), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal .SetRange Range("A1:D100") .Header = xlYes .Apply End With End Sub - Run the macro. Place the cursor inside the subroutine and press F5, or go back to Excel and use Developer > Macros, select the macro, and click Run. The table reorders immediately.
- Check the result. Confirm the header stayed on top and that entire rows moved together. If only one column sorted, your range or key is wrong; see Common Mistakes.
- Save as a macro-enabled workbook. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file discards the code when closed.
Common Mistakes
- Sorting only the key column. If the range passed to SetRange is a single column, Excel sorts that column alone and scrambles row relationships. Always set the range to the full table block.
- Forgetting SortFields.Clear. Reusing a Sort object without clearing carries over previous sort keys, producing unexpected multi-level order.
- Wrong header setting. Header:=xlNo tells Excel the first row is data, so your column titles get sorted into the middle of the table.
- Using the wrong column reference. ListColumns("Name") needs the exact header text. A trailing space in the header causes a run-time error 9 (subscript out of range).
- Sorting text that looks like numbers. Values stored as text sort character by character (so 10 comes before 2). Convert them to real numbers first, or use a helper column.
- Running the macro on the wrong sheet. ThisWorkbook.Worksheets("Sheet1") fails if the sheet is renamed. Use the sheet's actual tab name or refer to ActiveSheet deliberately.
Tips
- Record a macro while sorting manually once, then read the generated code. It shows the exact property names Excel expects for your version.
- Sort by multiple columns by adding more SortFields.Add lines before .Apply. The first added key is the primary sort.
- Use a variable for the table name so the same macro can handle several tables: Set tbl = ActiveSheet.ListObjects(1) refers to the first table on the sheet.
- Wrap the sort in Application.ScreenUpdating = False and restore it to True at the end to speed up large tables.
- If the table has calculated columns, sorting is safe, but structured references elsewhere in the workbook should be checked afterward.
- Menu labels and dialog wording vary slightly between Excel versions and platforms; the VBA object model is more consistent, which is why the code approach travels well.
Troubleshooting
- Run-time error 9, subscript out of range. The table or sheet name in your code does not match the workbook. Check spelling and any spaces, then update the string.
- Run-time error 1004. Usually the range is invalid or the workbook is protected. Unprotect the sheet (Review > Unprotect Sheet) or adjust the range address.
- Nothing happens. The macro may be in a different workbook than the active one. Confirm ThisWorkbook points where you expect, or switch to ActiveWorkbook.
- Header row sorted as data. Set Header to xlYes and make sure SetRange includes the header row.
- Rows scrambled. The sort range was narrower than the data. Widen SetRange to cover every column in the block.
- Macros disabled. Check the yellow security bar and enable content, or adjust Trust Center settings for the file location. Only enable macros in files you trust.
Frequently Asked Questions
Can I sort a table without knowing its name?
Yes. Use ActiveSheet.ListObjects(1) to refer to the first table on the active sheet, or loop through ListObjects to find the one you want.
How do I sort descending instead?
Change Order:=xlAscending to Order:=xlDescending in the SortFields.Add line.
Does sorting with VBA keep formulas and formatting?
Yes, sorting moves entire rows, so formulas and cell formatting travel with their rows. Relative references adjust as normal.
Why does my numeric column sort as text?
The cells are probably stored as text. Convert them to numbers (Data > Text to Columns, or multiply by 1 in a helper column) before sorting.
Can I sort by more than one column?
Yes. Add additional SortFields.Add lines before .Apply; the order you add them sets the sort priority.
Will the macro work on Excel for Mac?
The VBA object model is the same, so the code runs on Mac Excel, though file paths and some ribbon locations differ.