Custom Sort when worksheet changes

Andy15

Board Regular
Joined
Apr 1, 2017
Messages
56
I am looking for some advice.

I have some code that will sort the columns exactly as I need but currently sorts in ascending order.

I am looking to modify the code to sort by a custom order.

I recorded a macro to try and work out the code but when I place the recorded bit of code in my working section I get a Named argument not found.

Please find both pieces of code below.


Code:
Sub sort1()



Dim ColNum As Long


For ColNum = 2 To Cells(21, Columns.Count).End(xlToLeft).Column Step 3


    With Columns(ColNum)
        With Range(.Cells(21, 1), .Cells(Rows.Count, 3).End(xlUp))
            .sort Key1:=.Cells(21, 1), Order1:=xlAscending, Header:=xlYes
             
        End With
    End With


Next ColNum


End Sub



Modified code below

Code:
Sub sort1()



Dim ColNum As Long


For ColNum = 2 To Cells(21, Columns.Count).End(xlToLeft).Column Step 3


    With Columns(ColNum)
        With Range(.Cells(21, 1), .Cells(Rows.Count, 3).End(xlUp))
            .sort Key1:=.Cells(21, 1), Order1:=xlAscending, CustomOrder:= _
        "10115960,902240,11241290,10080910,11241460,10237680,11241500,10818620,10005390", Header:=xlYes
             
        End With
    End With


Next ColNum


End Sub

Also is there a way to make the code run automatically when a worksheet is updated

Thanks
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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