custom sort not applying

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
529
I've recorded a custom sort.
The sort works exactly how I want, when manually applied (during record process), but fails to apply when applied by executing recorded vba code; by fail, I mean not applying sort and no error messages displayed.
After the code has completed, I can visit the "Custom Sort" GUI and see my custom sort detailed in there (vba is correctly detailing my sort levels); Pressing ok from this screen, produces a sort warning to which I choose ok and then my sort actually applies.

The key piece that appears to be failing to execute, is the applying of the sort. Any idea's?

Code:
    ActiveWorkbook.Worksheets("Orders").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Orders").Sort.SortFields.Add2 Key:=Range( _
        "M2:M9808"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    ActiveWorkbook.Worksheets("Orders").Sort.SortFields.Add2 Key:=Range( _
        "A2:A9808"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Orders").Sort.SortFields.Add2 Key:=Range( _
        "E2:E9808"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Orders").Sort
        .SetRange Range("A1:O9808")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Change Add2 to Add and I added a variable to get the last row, test if it works for you.

Code:
Sub test()
  Dim lr As Long
[COLOR=#0000cd]  lr = Range("M" & Rows.Count).End(xlUp).Row[/COLOR]
  With ActiveWorkbook.Worksheets("Orders").Sort
    .SortFields.Clear
    .SortFields.[COLOR=#0000ff]Add [/COLOR]Key:=Range("M2:M" & lr), SortOn:=xlSortOnValues, _
      Order:=xlAscending, DataOption:=xlSortTextAsNumbers
    .SortFields.[COLOR=#0000ff]Add [/COLOR]Key:=Range("A2:A" & lr), SortOn:=xlSortOnValues, _
      Order:=xlAscending, DataOption:=xlSortNormal
    .SortFields.[COLOR=#0000ff]Add [/COLOR]Key:=Range("E2:E" & lr), SortOn:=xlSortOnValues, _
      Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange Range("A1:O" & lr)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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