Excel VBA Sort using a non standard method (custom)

dtrynoski

New Member
Joined
Aug 23, 2017
Messages
9
Need help. I have a spreadsheet where I need to sort my data by column F using a specific text order (red, yellow, green, hold, complete). Whats the best method to accomplish this? Can it be done using Range? or do I have to create an array?

I've specified my dataset range as follows:

FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A2:H" & FinalRow).Select
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I believe I found the answer, but maybe there is a better way. This is what I'm using:

With Application
.AddCustomList ListArray:=Array("Red", "Yellow", "Green", "Hold", "Complete")
x = .CustomListCount
Range("A2:H" & FinalRow).Sort Key1:=Range("F2"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=x, MatchCase:=False, Orientation:=xlTopToBottom
.DeleteCustomList ListNum:=x
End With
 
Upvote 0
Welcome to the forum.

Try:

Code:
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("A2"), CustomOrder:="red,yellow,green,hold,complete"
        .SetRange Range("A:A")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .Apply
    End With

Looks like you found a solution, but you can include the custom list in the sort itself if you want.
 
Upvote 0

Forum statistics

Threads
1,215,261
Messages
6,123,930
Members
449,134
Latest member
NickWBA

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