Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Sort Ascending: Step-by-Step Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To sort a range ascending in Excel VBA, use the Sort method on a Range object and set SortOrder to xlAscending. The shortest working version is Range("A1:C20").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes. That one line sorts the block A1:C20 by the values in column A, smallest to largest or A to Z, and keeps row 1 as a header. Everything below explains the pieces of that line, how to make it resilient when data size changes, and what to do when it errors out.

What You'll Need

  • Excel on Windows or Mac with macros enabled (a macro-enabled workbook, .xlsm, if you plan to save the code).
  • Access to the VBA editor: press Alt+F11 on Windows, or Option+F11 on Mac. On newer Mac builds you may instead need Tools > Macro > Visual Basic Editor from the ribbon.
  • A worksheet with data laid out in columns, ideally with a header row and no blank rows in the middle of the block.
  • Basic comfort writing a Sub procedure, or willingness to copy one and change the range references.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). If nothing happens on Mac, use the Developer tab, or enable it first under Excel > Preferences > Ribbon & Toolbar. Menu names vary slightly by Excel version, so treat these paths as a guide rather than exact wording.
  2. Insert a module. In the Project Explorer, right-click your workbook name and choose Insert > Module. Code must live in a standard module, not in a sheet or ThisWorkbook module, for a general-purpose sorting macro.
  3. Start the procedure. Type Sub SortAscending() and press Enter. The editor adds End Sub automatically.
  4. Point at the data. Add a line that selects the block you want sorted, for example Range("A1:C50").Sort. Include every column that belongs to a record, not just the key column, or you will sort part of each row and scramble the data.
  5. Set the sort key. Add Key1:=Range("A2") so the sort uses column A. You can reference the whole column with Range("A:A"), but a specific cell inside the block is easier to read and less error-prone.
  6. Set the order. Add Order1:=xlAscending. For dates and numbers this means oldest/smallest first; for text it means A to Z. Case sensitivity comes from the sort options, not from this argument.
  7. Tell Excel whether there is a header. Add Header:=xlYes if row 1 contains column titles, or Header:=xlNo if the first row is data. If you get this wrong, the header text gets sorted into the middle of your data as if it were a value.
  8. Assemble and run. Your finished line looks like this:
    Sub SortAscending()
        Range("A1:C50").Sort Key1:=Range("A2"), _
            Order1:=xlAscending, Header:=xlYes
    End Sub
    Press F5 while the cursor is inside the Sub to run it, then switch back to Excel to confirm the order.
  9. Make the range dynamic. Hard-coded addresses break when rows are added. Replace the fixed range with Range("A1").CurrentRegion, which expands to the contiguous block around A1. If your data has blanks, use Range("A1").End(xlDown) style bounds or the worksheet's UsedRange instead, and be aware both have edge cases.
  10. Sort by several columns. Add Key2 and Order2 after the first pair. For example, sort by Region ascending, then by Sales descending: Key2:=Range("C2"), Order2:=xlDescending. Excel applies Key1 as the primary criterion and the later keys as tie-breakers.
  11. Capture the full syntax if you need more. The most reliable way to get every argument right is to sort manually once with the macro recorder on (Developer > Record Macro), then read the generated Sort statement and adapt it.

Common Mistakes

Sorting only one column. Range("A1:A50").Sort reorders column A and leaves the rest of each row untouched, so records no longer line up. Always pass the full record block, even though you sort by one key inside it.

Forgetting Header:=xlYes. The default behaviour depends on how Excel interprets the range, which is why explicit is better. A header row sorted as data usually ends up somewhere in the middle of the list, and it is easy to miss.

Referencing the key as a plain value. Key1:=Range("A2") is correct because the argument expects a Range. Writing Key1:=Range("A2").Value passes a value and raises errors.

Mixing sheets. A bare Range(...) refers to the active sheet. If your macro runs while another sheet is showing, you sort the wrong place. Qualify it: Worksheets("Data").Range("A1").CurrentRegion.Sort ...

Sorting merged cells. Merged cells in the sort block produce odd results or a run-time error. Unmerge them first; merged cells have the same address for every row they span, which confuses the sort.

Tips

  • Assign the macro to a button (Developer > Insert > Button) so you can re-sort fresh data with one click.
  • Turn off Application.ScreenUpdating = False before sorting large blocks and back on afterwards to speed up execution.
  • To ignore case, set MatchCase:=False in the Sort method arguments. To sort left to right across columns instead of top to bottom, add Orientation:=xlLeftToRight.
  • Store the key column in a variable or constant so the same code can be reused on different reports with a one-line edit.

Troubleshooting

Run-time error 1004, "The sort reference is not valid." The range and the key do not overlap. Check that the key cell lies inside the range you passed, and that the range is not empty.

"Method 'Range' of object '_Global' failed." Usually the active sheet is not the one you think, or the sheet name in a qualified reference is misspelled.

Error 438, "Object doesn't support this property or method." Often a misspelled argument such as Order1 written as Order, or a typo in xlAscending.

Nothing seems to happen. The macro may have run on a hidden sheet or a range outside the visible area. Step through with F8 and check each line's effect.

Data looks scrambled. Undo immediately with Ctrl+Z, then re-check the range width and the header flag. If you have already saved, restore from a backup copy; sorting is destructive.

It works once, then breaks. A fixed range like A1:C50 fails when the data grows. Switch to CurrentRegion and re-test.

Frequently Asked Questions

Can I sort ascending on more than one column?

Yes. Use Key1/Order1 for the primary column and Key2/Order2 for the tie-breaker. Excel sorts by the first key, then applies later keys when values are equal.

How do I sort dates or numbers rather than text?

The same xlAscending argument applies. What matters is that the cells actually contain dates or numbers, not text that looks numeric. Text-formatted numbers sort as strings, which puts "10" before "9".

Does sorting in VBA affect the whole workbook or just the sheet?

Only the range you specify on the sheet you reference. Other sheets are untouched unless your macro explicitly sorts them.

How can I sort without changing the original data?

Copy the block to another location first and sort the copy. Excel has no non-destructive sort option; the values in the target range are reordered in place.

Why does my macro fail when the sheet is protected?

Protected sheets block sorting. Unprotect the sheet before sorting and reprotect it afterwards, or allow sorting in the protection settings when you set it up.

Can I sort by a custom list such as weekdays?

Yes. Use Order1:=xlAscending together with CustomOrder in the Sort arguments, or define the custom list in Excel options and reference it. This is less common and worth recording a macro for, since the exact argument is easy to get wrong.