Custom sort a table using multiple criteria

Diving_Dan

Board Regular
Joined
Oct 20, 2019
Messages
149
Hi again,

I have a table of data containing staff information that I need to custom sort however i'm struggling to find anything online to do what I need.

The Table is called Table1 and it is on sheet "A Team". I would like to custom sort column "B" which has the header "Department" in the order: "Warehouse", "Back Office", "Hospitality", "Customer Services". I then want to sort column "D" ascending which has the header "Surname" and then by column "E" which has the header "First Name".

As always thanks in advance.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

MoshiM

Active Member
Joined
Jan 31, 2018
Messages
371
Office Version
  1. 2016
Platform
  1. Windows
Edited to add an equal sign.
VBA Code:
Sub CustomSort_Diving_Dan()

Dim TBO As ListObject, C_B As Long, WS As Worksheet

Application.AddCustomList Array("Warehouse", "Back Office", "Hospitality", "Customer Services")

C_B = Application.CustomListCount

Set WS = ThisWorkbook.Worksheets("A Team")
Set TBO = WS.ListObjects("Table1")

With TBO
   
    With .Sort
       
        With .SortFields
           
            .Add Key:=WS.Cells(1, 2), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:=C_B
            .Add Key:=WS.Cells(1, 4), SortOn:=xlSortOnValues, Order:=xlAscending
            .Add Key:=WS.Cells(1, 5), SortOn:=xlSortOnValues, Order:=xlAscending
           
        End With
       
        .MatchCase= False
        .Header = xlYes
        .Apply
       
    End With

End With

Application.DeleteCustomList C_B

End Sub
 

Diving_Dan

Board Regular
Joined
Oct 20, 2019
Messages
149
That works perfectly thanks.

If I wanted to custom sort column G first using a different criteria how would I create and add in that custom list?
 

Watch MrExcel Video

Forum statistics

Threads
1,130,171
Messages
5,640,579
Members
417,151
Latest member
ChickenTenderer

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
Top