Excel VBA: Sort a Range by Column (Step-by-Step)
In this article
To sort a range by a column in Excel VBA, use the Sort method on a Range object, set the key to the column you want with Key1, and call Apply. The shortest working pattern is: Range("A1:D20").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlYes. That single line sorts rows A1:D20 by column B, keeping each row intact. The rest of this guide explains how to build that line safely, how to choose the right key column, and what to check when the sort does not behave as expected.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
- A workbook saved as
.xlsmif you want to keep the macro. - A data range with a clear header row, so
Header:=xlYesworks correctly.
Menu labels can differ slightly by Excel version, device and region, but the VBA syntax below is stable across current desktop versions.
Step-by-Step
- Open the VBA editor. Press
Alt+F11(Windows) orFn+Option+F11(Mac), or go to Developer > Visual Basic. - Insert a module. In the editor, choose Insert > Module. This is where your macro will live.
- Write the Sub. Start with
Sub SortByColumn()and end withEnd Sub. Everything else goes between those two lines. - Reference the range. Use an explicit range such as
Range("A1:D20"), or a dynamic one likeRange("A1").CurrentRegionwhich expands to the contiguous block around A1. AvoidCellsor whole columns unless you really mean the entire sheet. - Set the key column.
Key1must point to a single cell in the header row of the column you want to sort by, for exampleRange("B1")for column B. Do not point it at the whole column. - Choose the order. Use
Order1:=xlAscendingorOrder1:=xlDescending. For text, ascending is A to Z; for numbers, smallest to largest. - Tell Excel there is a header. Add
Header:=xlYesso the first row is not sorted into the data. UsexlNoonly if your range has no header. - Call Apply. The full line looks like:
Range("A1:D20").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlYes - Run the macro. Press
F5inside the Sub, or go back to Excel and use Developer > Macros, selectSortByColumn, and click Run. - Save the workbook as
.xlsmso the code is retained.
A complete example:
Sub SortByColumn()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
ws.Range("A1:D20").Sort _
Key1:=ws.Range("B1"), _
Order1:=xlAscending, _
Header:=xlYes
End Sub
Using a worksheet variable avoids ambiguity if the active sheet changes while the macro runs.
Common Mistakes
- Pointing Key1 at the wrong cell. Key1 must be a cell in the header row of the target column, not a data cell. If you use
Range("B2")withHeader:=xlYes, Excel may sort by the wrong field or throw an error. - Leaving out Header. Without
Header:=xlYes, Excel may treat the header row as data and sort it into the middle of the range. - Sorting only one column. If you select just column B and sort, only that column moves and rows become misaligned. Always include the full row width in the range.
- Using the active sheet implicitly.
Range("A1:D20")without a worksheet qualifier refers to the active sheet, which may not be the one you expect. Qualify withws.Range. - Forgetting to call Apply. Setting sort properties without
.Apply(or without using the one-line.Sortcall) leaves the data unchanged. - Merged cells in the range. Merged cells break sorting. Unmerge them first.
Tips
- Use
CurrentRegionfor data that grows:ws.Range("A1").CurrentRegion.Sort ...picks up the whole contiguous block automatically. - Sort by more than one column by adding
Key2,Order2, and so on, up toKey3in a single Sort call. - Turn off screen updating for large ranges:
Application.ScreenUpdating = Falsebefore the sort and back toTrueafterwards. - If your data has no header row, set
Header:=xlNoand pointKey1at a cell in the first data row. - For case-sensitive sorting, add
MatchCase:=Trueto the Sort call.
Troubleshooting
If the macro runs but nothing changes, check that you included .Apply or used the one-line .Sort form, and that the range actually contains the data you think it does. If you get a "Sort method of Range class failed" error, the most common cause is a mismatched Key1 cell, merged cells in the range, or a protected sheet. Unprotect the sheet first, then retry. If the header row ends up sorted into the data, add Header:=xlYes. If only one column moves, widen the range to include all columns in each row. If rows appear shuffled incorrectly, check whether any hidden rows or filtered rows are inside the range; clear filters before sorting. If the macro sorts the wrong sheet, qualify the range with an explicit worksheet object as shown above.
Frequently Asked Questions
Can I sort by a column without a header row?
Yes. Set Header:=xlNo and point Key1 at a cell in the first data row of the column you want to sort by.
How do I sort descending?
Change Order1:=xlAscending to Order1:=xlDescending. The rest of the line stays the same.
Can I sort by more than one column?
Yes. Add Key2:=ws.Range("C1"), Order2:=xlAscending to the same Sort call. Excel supports up to three keys in one call.
Why does my macro sort the wrong sheet?
Because an unqualified Range refers to the active sheet. Qualify it with a worksheet variable, for example ThisWorkbook.Worksheets("Sheet1").Range("A1:D20").
Does this work on Excel for Mac?
Yes, the VBA Sort method works on desktop Excel for Mac. The keyboard shortcut to open the editor differs, and the Developer tab is enabled through Excel > Preferences rather than File > Options.
Can I sort a table instead of a range?
Yes. Reference the table's range, for example ws.ListObjects("Table1").Range.Sort ..., or simply sort the underlying range that the table occupies.