VBA Sort limited limited to 3?

LNG2013

Active Member
Joined
May 23, 2011
Messages
466
I have not found any clear answer to this but is there a way to sort by more then 3 selections.

I want to sort by Date, Team, User, Item, then Sub iTEM
 
Just to clarify -

Below is the code I am using to sort... When adding a Key4 it fails

Code:
    Sub SortData()
    Range("A2:FR29921").Sort Key1:=Range("C2"), Order1:=xlDescending, Key2:=Range("F2"), _
     Order2:=xlAscending, Key3:=Range("D2"), Order3:=xlAscending, Key4:=Range("G2"), Order4:=xlAscending, Header:= _
        xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
        xlSortNormal
'Objective5m.Range("A1").AutoFilter
End Sub
 
Upvote 0
Just to clarify -

Below is the code I am using to sort... When adding a Key4 it fails

Code:
    Sub SortData()
    Range("A2:FR29921").Sort Key1:=Range("C2"), Order1:=xlDescending, Key2:=Range("F2"), _
     Order2:=xlAscending, Key3:=Range("D2"), Order3:=xlAscending, Key4:=Range("G2"), Order4:=xlAscending, Header:= _
        xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
        xlSortNormal
'Objective5m.Range("A1").AutoFilter
End Sub


I think 3 is the limit, does this help?

http://www.mrexcel.com/archive/Data/1401.html

Or you could just add another sort:


Code:
Sub LNG2013()

With  Range("A2:FR29921")
     .Sort Key1:=Range("C2"), Order1:=xlDescending, Key2:=Range("F2"), _
     Order2:=xlAscending, Key3:=Range("D2"), Order3:=xlAscending, Header:= _
        xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
        xlSortNormal
        
       .Sort Key1:=Range("4th Key"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
        
End With

End Sub
 
Upvote 0
Yeah I just ended up adding a second sort, but it is limited to 3, doesn't make much sense to have such a short limit but... adding another works ok.
 
Upvote 0
If you are using xl2007 or later (or for anyone who finds this post through a search)

Use SortFields.Add Method instead of

.Sort Key1:=, Key2:=, Key3:=

You can find instuctions for SortFields.Add Method in VBA Help.
 
Upvote 0
If you are using xl2007 or later (or for anyone who finds this post through a search)

Use SortFields.Add Method instead of

.Sort Key1:=, Key2:=, Key3:=

You can find instuctions for SortFields.Add Method in VBA Help.

Thanks for the tip Jerry. I use xl2003 at work. I actually have xl2007 at home. I just don't use it that much, but it's good too know.
 
Upvote 0
Another way:
Insert a helper column in column A
in this column, concatenate the values in the columns to be sorted, in the order that you want them sorted. - use a formula eg =B2&E2&D2&C2&G2, where the columns to be sorted are
B then E then D then C then G.
fill formula to end of data
copy, paste values in column A
sort by column A
delete column A
 
Upvote 0
I did find this thread via a search, and I thank you all for your thorough answers.

But....

surely performing a second iteration of sort will, to an extent, override the first? So that, in practice Keys "4-6" are the primary keys, and "1-3" are the secondary keys.

Or have I missed something fundamental?
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top