0

I have a Macro that sorts a database column by the category name with a custom order. I'm not sure if it is possible but I would like to use a defined name that contains all of the category names that is called Sorting_List. I would like for the defined name Sorting_List to replace the "FL,MF,HF,Furnace Eng,Launder,CM,CM Eng,ML,PP,Lab SOT,Lab AT,SSP,SII,NAPS,ES,FM,PW,C2H2,CA,Fluids,Fluids Eng,Elec,PMDA,Elec Eng,CH SW,CH SII,CH Std,CH High,CH Elec,CH Eng,CH FS,FS SW,FS SII,FS Elec,Eng,Train,Lab,Lab SW" so if I ever change anything in that defined name list it will automatically change in my macro. Below is my code. Any help is much appreciated.

  ActiveWorkbook.Worksheets("Database").ListObjects("Database").Sort.SortFields. _
    Add Key:=Range("Database[Category]"), SortOn:=xlSortOnValues, Order:= _
    xlAscending, CustomOrder:= _
    "FL,MF,HF,Furnace Eng,Launder,CM,CM Eng,ML,PP,Lab SOT,Lab AT,SSP,SII,NAPS,ES,FM,PW,C2H2,CA,Fluids,Fluids Eng,Elec,PMDA,Elec Eng,CH SW,CH SII,CH Std,CH High,CH Elec,CH Eng,CH FS,FS SW,FS SII,FS Elec,Eng,Train,Lab,Lab SW" _
    , DataOption:=xlSortNormal

1 Answer 1

1
Dim sCustomOrder as String
Dim rng as Range
sCustomOrder = ""
For Each rng in ActiveWorkbook.Names("Sorting_List").RefersToRange
    If rng.Text <> "" Then sCustomOrder = sCustomOrder & "," & rng.Text
Next rng

ActiveWorkbook.Worksheets("Database").ListObjects("Database").Sort.SortFields.Add _
    Key:=Range("Database[Category]"), _
    SortOn:=xlSortOnValues, _
    Order:= xlAscending, _
    CustomOrder:= sCustomOrder, _
    DataOption:=xlSortNormal
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. However, it keeps debugging at the ActiveWorkbook.Worksheets line
The only way anyone can be helpful here is to know exactly what is happening. What exactly is the error message that is being displayed before you click "Debug"?

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.