Sort by level (macro not working)

gomes123

New Member
Joined
Jun 16, 2021
Messages
35
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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
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
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,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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