getting a Compile Error

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello guys
I am trying to edit JohnnyL's code and add one more level in the sort data but I keep getting an error. Can someone please tell me what am I doing wrong here.?
JohnnyL's code
VBA Code:
'   RANGE SORTER ... Most important column to least important column 3,6,2
    With wsDestination
        .Range("A2:O" & .Range("B" & Rows.Count).End(xlUp).Row).Sort _
                Key1:=.Range("C2"), Order1:=xlAscending, _
                Key2:=.Range("F2"), Order1:=xlAscending, _
                Key3:=.Range("B2"), Order1:=xlAscending, Header:=xlNo                               '   Sort the destination sheet by various columns
    End With
I tried doing this but I am getting a compile error.
Code:
RANGE SORTER ... Most important column to least important column 3,6,2
    With wsDestination
        .Range("A2:O" & .Range("B" & Rows.Count).End(xlUp).Row).Sort _
                Key1:=.Range("C2"), Order1:=xlAscending, _
                Key2:=.Range("G2"), Order1:=xlAscending, _
                Key3:=.Range("H2"), Order1:=xlAscending, Header:=xlNo                               '   Sort the destination sheet by various columns
                Key4:=.Range("B2"), Order1:=xlAscending, Header:=xlNo                               '   Sort the destination sheet by various columns
    End With
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I just corrected the above as
Rich (BB code):
RANGE SORTER ... Most important column to least important column 3,6,2
    With wsDestination
        .Range("A2:N" & .Range("B" & Rows.Count).End(xlUp).Row).Sort _
                Key1:=.Range("C2"), Order1:=xlAscending, _
                Key2:=.Range("G2"), Order1:=xlAscending, _
                Key3:=.Range("H2"), Order1:=xlAscending, _
                Key4:=.Range("B2"), Order1:=xlAscending, Header:=xlNo                               '   Sort the destination sheet by various columns
    End With
but still I am getting a compile error at
Rich (BB code):
                Key4:=.Range("B2"), Order1:=xlAscending, Header:=xlNo                               '   Sort the destination sheet by various columns
stating named argument not found.
 
Upvote 0
You won't be able to sort more than 3 columns using the Sort method of the Range object. Use the Sort object instead...

VBA Code:
    Dim sortRange As Range
    With wsDestination
        Set sortRange = .Range("A2:N" & .Cells(.Rows.Count, "B").End(xlUp).Row)
    End With
   
    With wsDestination.Sort.SortFields
        .Clear
        .Add2 Key:=Range("C2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add2 Key:=Range("G2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add2 Key:=Range("H2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add2 Key:=Range("B2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    End With
   
    With wsDestination.Sort
        .SetRange sortRange
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

Hope this helps!
 
Upvote 0
Solution
You won't be able to sort more than 3 columns using the Sort method of the Range object. Use the Sort object instead...

VBA Code:
    Dim sortRange As Range
    With wsDestination
        Set sortRange = .Range("A2:N" & .Cells(.Rows.Count, "B").End(xlUp).Row)
    End With
  
    With wsDestination.Sort.SortFields
        .Clear
        .Add2 Key:=Range("C2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add2 Key:=Range("G2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add2 Key:=Range("H2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add2 Key:=Range("B2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    End With
  
    With wsDestination.Sort
        .SetRange sortRange
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

Hope this helps!
It really helped. Thanks Domenic.😂 Now I can add or remove levels and then too it will work I hope. Will try it out.
 
Upvote 0
It really helped. Thanks Domenic.😂
You're very welcome, glad I could help.
Now I can add or remove levels and then too it will work I hope. Will try it out.
It looks like you can have up to 64 levels...


Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,017
Members
448,937
Latest member
BeerMan23

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