Auto sort with custom list

mariposa

New Member
Joined
Sep 14, 2016
Messages
14
I can get my table to auto-sort perfectly upon changes in ascending or descending order using the following code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Not Intersect(Target, Range("E:E")) Is Nothing Then
        Range("E8").Sort Key1:=Range("E9"), _
          Order1:=xlAscending, Header:=xlYes, _
          MatchCase:=False, _
          Orientation:=xlTopToBottom
    End If
End Sub
However, I need it in a custom order, so I have tried the following (along with many others ways, but these were the shortest) but cannot get it to work.
Help please and thank you in advance.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Not Intersect(Target, Range("E:E")) Is Nothing Then
        Range("E8").Sort Key1:=Range("E9"), _
          SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:="DM,CM,Admin/Clerk,Maint", _
        DataOption:=xlSortNormal, Orientation:=xlTopToBottom
    End If
End Sub
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Not Intersect(Target, Range("E:E")) Is Nothing Then
        Range("E8").Sort Key1:=Range("E9"), _
        Order:=xlAscending, CustomOrder:="DM,CM,Admin/Clerk,Maint", _
        OrderCustom:=1, DataOption:=xlSortNormal, Orientation:=xlTopToBottom
    End If
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Welcome to the Board!

Are you able to get it to sort properly if you do it manually?
If so, then if you turn on the Macro Recorder and record yourself doing that, it should give you the block of code you need to insert into your current procedure.
 
Upvote 0
OMG! Awesome it worked. Originally I tried it yesterday and it did not work. I must not have altered the code and missed something the first time.
So now my full code looks like the following (this way if someone needs help on the same):
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Not Intersect(Target, Range("E:E")) Is Nothing Then
    ActiveSheet.Unprotect "password"
    ActiveWorkbook.Worksheets("sheet name").ListObjects("table name").Sort. _
        SortFields.Clear
    ActiveWorkbook.Worksheets(("sheet name").ListObjects("table name").ort. _
        SortFields.Add Key:=Range("table name[header name]"), SortOn:= _
        xlSortOnValues, Order:=xlAscending, CustomOrder:= _
        "DM,CM,Admin/Clerk,Maint" _
        , DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("sheet name").ListObjects("table name").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    End If
        ActiveSheet.Protect "password", DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFormattingCells:=True, AllowFormattingColumns:=True, _
        AllowInsertingColumns:=True, AllowDeletingRows:=True, AllowSorting:=True _
        , AllowFiltering:=True
End Sub
As you can see this also has a password protect feature along with the change trigger.

I must say I love this site!
Thank you, Joe for your help and pointing back on track. :biggrin:
 
Upvote 0

Forum statistics

Threads
1,216,085
Messages
6,128,732
Members
449,465
Latest member
TAKLAM

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