Excel macro that executes a multi-level custom sort using a custom list for ordering

289 Views Asked by At

I am trying to create a macro that executes a two-level sort using a custom list for a dynamic table.

  • Level 1: Sort by (Range "C2") // Sort on (Cell Values) // Order (Oldest to Newest)
  • Level 2: Sort by (Range "D2") // Sort on (Cell Values) // Order (Custom List)

The table is updated on a daily basis and, therefore, requires the macro to capture a varying number of rows.

The Custom List that I would like to order the Location column by is: "Greater London, Liverpool, Birmingham, Manchester, Sheffield, Leeds, Bristol" - (in reality my custom list is rather long and consists of various strings).

The below is what I have written so far and would appreciate any input. Thanks!

Sub CustomSort()

Range("B2").CurrentRegion.Sort _ 
key1:= Range("C2"), Order1:=xlAscending, Orientation:=xlTopToBottom, Header:=xlYes, _
key2:= Range("D2"), Order1:=xlAscending, Orientation:=xlTopToBottom, Header:=xlYes, CustomOrder:= _
"Greater London,Liverpool,Birmingham,Manchester,Sheffield,Leeds,Bristol"

End Sub

enter image description here

0

There are 0 best solutions below