Custom sort a table using multiple criteria

Diving_Dan

Board Regular
Joined
Oct 20, 2019
Messages
161
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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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
 
Upvote 0
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?
 
Upvote 0

Forum statistics

Threads
1,214,851
Messages
6,121,931
Members
449,056
Latest member
denissimo

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