Sort by level (macro not working)

gomes123

New Member
Joined
Jun 16, 2021
Messages
19
Office Version
  1. 2007
Platform
  1. Windows
I'm trying to automate the sort function in excel (by levels).
So I would like to first sort Column B by A-Z
Then the next level is sort Column N from oldest to newest date

The problem with the macro below, is it sorts Column B fine (A-Z), but then it doesn't apply the "level" criteria, and it just jumbles up the dates in Column N (from oldest to newest), and it doesn't tally with the data in Column B. How would I fix this? Most appreciated, thanks.
VBA Code:
Sub SortColumnsBAndN()
    'Sort Column B by A-Z
    Columns("B:B").Sort key1:=Range("B2"), Order1:=xlAscending, Header:=xlYes
    
    'Sort Column N by oldest to newest date
    Columns("N:N").Sort key1:=Range("N2"), Order1:=xlAscending, Header:=xlYes, _
    MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortTextAsNumbers, _
    Header:=xlYes
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You need to add all the sort fields first, and then sort the whole thing by the two keyfields.

For example.

VBA Code:
Sub SortColumnsBAndN()
    ActiveSheet.Sort.SortFields.Add2 Key:=Range("B2") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveSheet.Sort.SortFields.Add2 Key:=Range("N2") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("B:N")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Regards

Murray
 
Upvote 1
Thanks for your help, but I'm getting a runtime error 438 when I run your code, on the line
VBA Code:
 SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
. Is there something I'm missing?
 
Upvote 0
The OP is also using 2007 which is why I mentioned the Add2
 
Upvote 0
You need to add all the sort fields first, and then sort the whole thing by the two keyfields.

For example.

VBA Code:
Sub SortColumnsBAndN()
    ActiveSheet.Sort.SortFields.Add2 Key:=Range("B2") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveSheet.Sort.SortFields.Add2 Key:=Range("N2") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("B:N")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Regards

Murray
Thanks, after changing to Add from Add2, it works!
Try changing SortFields.Add2 to SortFields.Add

Thanks! It worked after changing it to Add, from Add2.
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,970
Members
449,095
Latest member
Mr Hughes

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