Smart Systems for Home

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

Excel VBA: Sort a Range by Column (Step-by-Step)

· 5 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 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 .xlsm if you want to keep the macro.
  • A data range with a clear header row, so Header:=xlYes works 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

  1. Open the VBA editor. Press Alt+F11 (Windows) or Fn+Option+F11 (Mac), or go to Developer > Visual Basic.
  2. Insert a module. In the editor, choose Insert > Module. This is where your macro will live.
  3. Write the Sub. Start with Sub SortByColumn() and end with End Sub. Everything else goes between those two lines.
  4. Reference the range. Use an explicit range such as Range("A1:D20"), or a dynamic one like Range("A1").CurrentRegion which expands to the contiguous block around A1. Avoid Cells or whole columns unless you really mean the entire sheet.
  5. Set the key column. Key1 must point to a single cell in the header row of the column you want to sort by, for example Range("B1") for column B. Do not point it at the whole column.
  6. Choose the order. Use Order1:=xlAscending or Order1:=xlDescending. For text, ascending is A to Z; for numbers, smallest to largest.
  7. Tell Excel there is a header. Add Header:=xlYes so the first row is not sorted into the data. Use xlNo only if your range has no header.
  8. Call Apply. The full line looks like:
    Range("A1:D20").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlYes
  9. Run the macro. Press F5 inside the Sub, or go back to Excel and use Developer > Macros, select SortByColumn, and click Run.
  10. Save the workbook as .xlsm so 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") with Header:=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 with ws.Range.
  • Forgetting to call Apply. Setting sort properties without .Apply (or without using the one-line .Sort call) leaves the data unchanged.
  • Merged cells in the range. Merged cells break sorting. Unmerge them first.

Tips

  • Use CurrentRegion for 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 to Key3 in a single Sort call.
  • Turn off screen updating for large ranges: Application.ScreenUpdating = False before the sort and back to True afterwards.
  • If your data has no header row, set Header:=xlNo and point Key1 at a cell in the first data row.
  • For case-sensitive sorting, add MatchCase:=True to 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.