Excel VBA Sort: More Than Three Columns Made Easy
In this article
Excel's built-in sort dialog only allows three sort levels, but you can sort by more than three columns in VBA by adding multiple SortFields to a single Sort object. The limit is not three—it is 64 sort fields per sort operation. The trick is to use the Sort method with Key1, Key2, Key3 and then add additional keys with SortFields.Add. This guide shows the exact syntax, a working example, and the pitfalls that cause run-time errors.
What You'll Need
- Excel 2010 or later (the
SortFieldsobject was introduced in Excel 2010; earlier versions need a different approach). - A worksheet with data in a contiguous range, ideally with a header row.
- Basic familiarity with the VBA editor (Alt+F11) and how to insert a module.
- Macro-enabled workbook (.xlsm) if you want to save the code.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or use the Developer tab). In the Project Explorer, right-click your workbook and choose Insert > Module. Labels can differ slightly by Excel version and region.
- Define the range. Set a
Rangevariable to the data including headers. For example:Dim rng As Range Set rng = ActiveSheet.Range("A1:F100")Using a fixed range is safest; if you useCurrentRegion, make sure there are no blank rows or columns inside the block. - Clear any existing sort state. Call
rng.Sort.SortFields.Clearbefore adding fields. If you skip this, Excel may keep old sort keys and produce unexpected order. - Add the first three sort fields using Key1, Key2, Key3. These are the only keys available as named arguments. For example:
rng.Sort Key1:=rng.Columns(1), Order1:=xlAscending, _ Key2:=rng.Columns(2), Order2:=xlAscending, _ Key3:=rng.Columns(3), Order3:=xlAscending, _ Header:=xlYesNote thatKey1etc. accept a Range, not a column letter. - Add the fourth and further keys with SortFields.Add. After the first three, use:
With rng.Sort .SortFields.Add Key:=rng.Columns(4), SortOn:=xlSortOnValues, _ Order:=xlAscending, DataOption:=xlSortNormal .SortFields.Add Key:=rng.Columns(5), SortOn:=xlSortOnValues, _ Order:=xlDescending, DataOption:=xlSortNormal .SetRange rng .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .Apply End WithTheSortFields.Addmethod has no built-in limit of three; you can add up to 64 fields total. - Set the range and header options on the Sort object. Use
.SetRange rngand.Header = xlYes(orxlNoif no headers). Without.SetRange, the sort may apply to the wrong area. - Apply the sort. Call
.Applyat the end. If you omit it, nothing happens. - Run the macro. Press F5 or assign the macro to a button. Check the result: rows should be ordered by column 1, then column 2, then column 3, then column 4, and so on.
Common Mistakes
- Using Key4, Key5 arguments. There is no
Key4named argument. OnlyKey1,Key2,Key3exist. Additional keys must go throughSortFields.Add. - Forgetting
SortFields.Clear. Old sort fields persist between runs and can cause a "Sort method of Range class failed" error or wrong order. - Passing a column letter to Key.
Key:="D"is invalid; userng.Columns(4)orRange("D1:D100"). - Not setting
Header. If headers are present butHeader:=xlNo, the header row gets sorted into the data. - Using
CurrentRegionwith blank rows. The region stops at the first blank row, so part of your data is left unsorted. - Mixing
Key1arguments withSortFields.Addincorrectly. If you useKey1in theSortmethod, do not also add the same field viaSortFields.Add—duplicate keys cause errors.
Tips
- Use
Withblocks to keep the code readable when adding many fields. - Store sort column numbers and orders in arrays if the sort criteria change often; loop to add fields.
- Set
MatchCasetoTrueonly if case-sensitive sorting is needed; default isFalse. - For text that looks like numbers, set
DataOption:=xlSortTextAsNumberson the relevant field. - Always test on a copy of the data first; sorting is destructive and cannot be undone with Ctrl+Z after macro execution.
- If you need to sort by more than 64 columns, split the operation into two passes or restructure the data.
Troubleshooting
If the macro fails with "Sort method of Range class failed", check that rng is a valid range on the active sheet and that SortFields.Clear was called. If the order is wrong, verify that each SortFields.Add uses the correct column index and that Header matches your data. If only the first three columns seem to sort, confirm you actually added the extra fields and called .Apply. If Excel hangs or errors on SortFields.Add, ensure you are not exceeding 64 fields and that no field key overlaps with Key1–Key3. For version differences, the SortFields object exists in Excel 2010 and later; in Excel 2007 and earlier, use multiple Sort calls in reverse order (least important first) as a workaround.
Frequently Asked Questions
Can I sort by more than three columns using the Excel interface?
The Sort dialog allows only three levels. To sort by more than three, you must use VBA with SortFields.Add or a helper column that concatenates keys.
What is the maximum number of sort fields in VBA?
Excel supports up to 64 sort fields per sort operation. The practical limit is usually far lower.
Why do I get "Sort method of Range class failed"?
Common causes: the range is invalid, SortFields were not cleared, or a key range does not match the sort range size.
Can I sort by a column not included in the range?
No. Every key must be a column within the range being sorted. Extend the range to include all key columns.
Does this work on Excel for Mac?
Yes, the VBA Sort object and SortFields work on Excel for Mac 2011 and later, though menu labels and shortcut keys differ.
How do I sort by custom lists (e.g., month names)?
Set Order:=xlAscending and use CustomOrder in SortFields.Add, e.g., CustomOrder:="Jan,Feb,Mar". This requires the custom list to be defined or supplied as a comma-separated string.