SORT WITH CUSTOM ORDER

oblix

Board Regular
Joined
Mar 29, 2017
Messages
183
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
  9. 2003 or older
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi there
I have the following code to sort according to a custom list:

VBA Code:
Sub Sort_perm()

    ActiveWorkbook.Worksheets("Perm Transfer").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Perm Transfer").Sort.SortFields.Add Key:=Range( _
        "Q3:Q1000"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
        "Brig Gen,Col,Lt Col,Maj,Capt,CO,WO1,WO2,F Sgt,Sgt,Cpl,L Cpl,Amn,Mr,Me" _
        , DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Perm Transfer").Sort
        .SetRange Range("O2:Z1000")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("Q8").Select
End Sub


Can someone please assist to clean up this code
Thank you in advance
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Your code works so not exactly sure what you had in mind, here is a minor variation.
VBA Code:
Sub Sort_perm()
    Dim lastRow As Long

    With ActiveWorkbook.Worksheets("Perm Transfer")
        lastRow = .Range("O" & Rows.Count).End(xlUp).Row            ' Pick appropriate column if not "O"
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Range( _
            "Q3:Q" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
            "Brig Gen,Col,Lt Col,Maj,Capt,CO,WO1,WO2,F Sgt,Sgt,Cpl,L Cpl,Amn,Mr,Me" _
            , DataOption:=xlSortNormal
        With .Sort
            .SetRange Range("O2:Z" & lastRow)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With

End Sub
 
Upvote 0
Solution
Thank you
Looks much more professional than mine
If someone types in other than on sort list will it be put at the bottom of list by default?
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,215,615
Messages
6,125,857
Members
449,266
Latest member
davinroach

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