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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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,214,525
Messages
6,120,051
Members
448,940
Latest member
mdusw

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