You sort an Excel range from Visual Basic for Applications (VBA) by calling the Range.Sort method, which is the same sorting engine Excel uses when you click a column header and choose Sort A to Z. The shortest working version is Range("A1:C20").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes. That single line sorts rows 1 through 20 by column A while keeping each row intact. The rest of this guide covers how to point the sort at the right range, how to sort by more than one column, how to handle headers, and what to do when the macro throws an error or scrambles your data.

What You'll Need

  • Microsoft Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
  • A workbook containing the data you want to sort, ideally saved before you run any macro.
  • Basic familiarity with opening the Visual Basic Editor (Alt+F11 on Windows, Option+F11 on Mac) and inserting a module.
  • Macro security set to allow macros in this workbook, or the code will not run.

Step-by-Step

  1. Save a backup copy of the workbook. Sorting rewrites cell contents in place. If the macro targets the wrong range, the original order is gone unless you have a copy. Use File > Save As and give it a different name before you start.
  2. Open the Visual Basic Editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project pane on the left, right-click your workbook name, choose Insert > Module. A blank code window appears.
  3. Write the simplest sort. Type a procedure such as:
    Sub SortByColumnA()
      Range("A1:C20").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
    End Sub
    Here Key1 is the column Excel sorts on, Order1 is the direction, and Header tells Excel whether row 1 contains labels. Use xlDescending for Z to A or largest to smallest.
  4. Match the range to your data. The range must include every column you want carried along with the sort key. If you sort A1:A20 only, columns B and C stay put and your rows no longer line up. A safer pattern is to let Excel detect the block: Range("A1").CurrentRegion.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes. CurrentRegion expands to the contiguous block around A1, which usually matches the table.
  5. Run the macro. Place the cursor inside the procedure and press F5, or go back to Excel and use Developer > Macros, select the name, and click Run. Watch the sheet: the rows should reorder while the header stays at the top.
  6. Add a second sort key for ties. When two rows share the same value in column A, Excel keeps their relative order unless you specify another key. Extend the call:
    Range("A1:C20").Sort Key1:=Range("A1"), Order1:=xlAscending, _
      Key2:=Range("B1"), Order2:=xlAscending, Header:=xlYes
    Excel supports up to three keys this way (Key1, Key2, Key3). For more levels, sort from the least important key to the most important, running the method several times.
  7. Sort by a value that is not in the first column. Point Key1 at any cell in the header row of the column you want, for example Key1:=Range("C1"). The range you sort must still contain column C.
  8. Handle text that looks like numbers. If a column of numbers is stored as text, ascending order can place "10" before "2". Convert the column first (Data > Text to Columns, Finish) or accept that Excel is sorting the characters, not the values.
  9. Test on a copy, then save. Run the macro on the duplicate workbook, confirm the row order and that no data shifted between columns, then save the file as an .xlsm macro-enabled workbook so the code is kept.

Common Mistakes

Sorting only one column. Selecting A1:A20 and sorting moves that column alone, breaking the link to the neighbouring cells. Always include the full width of the table in the range.

Forgetting Header:=xlYes. If you leave it out, Excel may treat the header row as data and sort it into the middle of the list. The default depends on context, so state it explicitly every time.

Hard-coding a fixed range. A range like A1:C20 silently misses rows added later. CurrentRegion or a dynamic last-row calculation avoids stale ranges.

Sorting inside a loop. Calling Sort once per row inside a For Each loop is slow and can produce odd results. Sort once, after all data is written.

Assuming the sort is stable across keys. Excel does not guarantee that equal values keep their original order unless you add a tie-breaker key.

Tips

Record a macro while you perform the sort manually (Developer > Record Macro), then read the generated code. It shows the exact arguments Excel expects for your version and locale, which is faster than guessing.

Use Application.ScreenUpdating = False before a long series of sorts and set it back to True afterwards; the screen will not flicker and the macro finishes sooner.

If your data sits in an Excel Table (ListObject), sort the table's Range rather than a plain range so the table structure and any formulas stay consistent.

Name your sort keys with variables when the column can move, for example Key1:=Range("A1").Offset(0, colIndex), so inserting a column does not break the macro.

Troubleshooting

Run-time error 1004. This usually means the range is invalid, the sheet is protected, or the key cell is outside the sorted range. Unprotect the sheet (Review > Unprotect Sheet) and confirm Key1 points inside the range.

Header row ends up in the middle. Set Header:=xlYes explicitly, or move the header out of the range and sort only the data rows.

Rows no longer match their labels. The sorted range was narrower than the table. Undo with Ctrl+Z immediately, then widen the range to include all columns.

The macro does nothing. Check that macros are enabled for the file, that the procedure is inside a standard module (not a sheet module), and that the workbook is not open read-only.

Sort order looks wrong for dates or numbers. The cells may be text. Select the column, use Data > Text to Columns and click Finish to coerce them to real values, then re-run the sort.

Frequently Asked Questions

Can I sort without changing the original sheet?

Sort works in place. To keep the original, copy the range to a new sheet or workbook first and sort the copy.

How many sort keys can I set in one call?

Three (Key1, Key2, Key3). For more levels, run the Sort method repeatedly, starting with the least important column.

Does Range.Sort work on Mac Excel?

Yes, the method and arguments are the same. Menu labels and the ribbon differ slightly, but the VBA code is identical.

Why does my sort put 10 before 2?

The cells are stored as text, so Excel compares characters. Convert them to numbers first, then sort.

Can I sort by cell colour or icon?

Yes, using the SortOn argument with values such as xlSortOnCellColor, plus the corresponding colour or icon argument. The exact constants vary by Excel version, so record a macro to capture the correct ones.

Will sorting break formulas that reference other cells?

Relative references inside the sorted rows move with the data. Absolute references to fixed cells stay put. Check any cross-sheet formulas after the first run.