VBA Sort multiple columns

maclachlan19

Board Regular
Joined
Jul 8, 2013
Messages
53
I'm trying to run some code to sort multiple columns at once. I get an error:
Compile error: Named argument not found


VBA Code:
Range("Managers").Sort Key1:=Range("Q4"), Order1:=xlDescending, _
                        Key2:=Range("G4"), Order2:=xlDescending, _
                        Key3:=Range("H4"), Order3:=xlAscending, _
                        Key4:=Range("F4"), Order4:=xlDescending, _
                        Key5:=Range("I4"), Order5:=xlAscending, _
                        Header:=xlYes
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You can only sort by three columns at a time.

You will need to do it in two stages.
 

Attachments

  • Error.JPG
    Error.JPG
    58.1 KB · Views: 67
Upvote 0
That makes sense on why in testing I could get the code to work with only 2 columns.
The problem is I can't break this into 2 stages as I need the sort to be done at the same time. If I use 2 stages the sort from the 1st stage is overridden by the 2nd stage.
 
Upvote 0
You could do something like

VBA Code:
With Range("Managers")
    .Sort (key1:=Range("I4"), Order1:= xlAscending)
    .Sort (key1:=Range("F4"), Order1:= xlDescending)
    .Sort (key1:=Range("H4"), Order1:= xlAscending)
    .Sort (key1:=Range("G4"), Order1:= xlDescending)
    .Sort (key1:=Range("Q4"), Order1:= xDescending)
End With

Note that the order of the keys of the 5 sorts is in reverse order of the keys in the single sort attempt.
 
Upvote 0
Thanks for the quick responses Mike.
When I run this code I get a "Compile error: Syntax error"
Do I use Key1 for all 5 lines?
 
Upvote 0
That error It may be the parenthesis.
What I posted is 5 different sorts, each with its own key, i.e. key1.
 
Upvote 0
I tried this below but same error, but perhaps I misunderstood your latest comment

VBA Code:
Private Sub CommandButton2_Click()
With Range("Managers")
    ,Sort key1:=Range("I4"), Order1:= xlAscending _
    ,Sort key1:=Range("F4"), Order1:= xlDescending _
    ,Sort key1:=Range("H4"), Order1:= xlAscending _
    ,Sort key1:=Range("G4"), Order1:= xlDescending _
    ,Sort key1:=Range("Q4"), Order1:= xDescending _
End With
 
Upvote 0
Yes, take out the parenthesis.

The last order should be xlDescending.
 
Upvote 0
my mistake,
I'm still getting the "Compile error: Syntax error" .
VBA Code:
Private Sub CommandButton2_Click()
With Range("Managers")
    ,Sort key1:=Range("I4"), Order1:= xlAscending _
    ,Sort key1:=Range("F4"), Order1:= xlDescending _
    ,Sort key1:=Range("H4"), Order1:= xlAscending _
    ,Sort key1:=Range("G4"), Order1:= xlDescending _
    ,Sort key1:=Range("Q4"), Order1:= xlDescending _
End With
 
Upvote 0

Forum statistics

Threads
1,223,692
Messages
6,173,858
Members
452,535
Latest member
berdex

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