VBA custom sort

aurelius89

Board Regular
Joined
Mar 15, 2017
Messages
69
I have recorded the following macro while doing my custom sort:

Code:
Sub sortMembers()


Dim ws As Worksheet: Set ws = Worksheets("RefindData")
    
ws.sort.SortFields.Clear
    
ws.sort.SortFields.Add Key:=Range( _
        "A2:A9998"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    
    
ws.sort.SortFields.Add Key:=Range( _
        "C2:C9998"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    
    
ws.sort.SortFields.Add Key:=Range( _
        "F2:F9998"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    
    
    With ws.sort
        .SetRange Range("A1:P9998")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With




End Sub

I have cleaned it up a little by removing selects and defining my worksheet as a variable. Any other way this can be made more efficient or cleaned up?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
One suggestion, but everyone has their own way/style/preferences:
Code:
Sub sortMembers()

    Dim LR  As Long
    Dim rng As Range
    Dim wks As Worksheet
    
    Application.ScreenUpdating = False
    
    'Set wks = Sheets("RefinedData")
    With ActiveSheet
        LR = .Cells(.Rows.Count, 1).End(xlUp).row - 1
        Set rng = .Cells(1, 1).Resize(LR + 1, 16)
        .Sort.SortFields.Clear
        .Sort.SortFields.add key:=.Cells(2, 1).Resize(LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.add key:=.Cells(2, 3).Resize(LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
        .Sort.SortFields.add key:=.Cells(2, 6).Resize(LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
        
        With .Sort
            .SetRange rng
            .header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
    
    Application.ScreenUpdating = True
    
    Set rng = Nothing
    Set wks = Nothing
        
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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