0

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

2
  • Regarding a VBA-solution: you will find the answer here: VBA, Sorting based on custom list. but if you have Excel 365 you can use the new SORTBY function as well (see e.g. exceljet.net/formula/sort-by-custom-list) Commented Oct 5, 2022 at 10:43
  • Your example is not appropriate, anyhow... The second sort makes sense only if **for the same Date in "C:C" there are many cases of different locations. Such a picture could help only if it is appropriate and followed by another one showing how you like the data to be sorted. You can see how to deal with the sorting itself in the link posted above... Commented Oct 5, 2022 at 11:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.