Macro Custom Sort

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,589
Office Version
  1. 2021
Platform
  1. Windows
I have data in Cols A to C (headers are in row 1). I have tried to write code to sort the data in Col C based on a custom order on sheet "Unique" . These are listed in Z1 to Z5 on this sheet.

I get a run time error "Application-defined or object-defined error" and the code below is highlighted

It would be appreciated if someone could amend my code

Code:
  .Range("A1:C" & Lr).Sort Key1:=.Range("C1"), Order1:=xlAscending, _
            customOrder:=customOrder, MatchCase:=False, Header:=xlYes



See full Code below


Code:
 Sub Sort_Bank_Accounts()
    Dim Lr As Long
    Dim customOrderRange As Range
    Dim customOrder() As Variant
    
    ' Set the custom order range
    Set customOrderRange = Sheets("Unique").Range("Z1:Z5")
    
    ' Read the custom order values into an array
    customOrder = customOrderRange.Value
    
    With Sheets("Bank Accounts")
        Lr = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Range("A1:C" & Lr).Sort Key1:=.Range("C1"), Order1:=xlAscending, _
            customOrder:=customOrder, MatchCase:=False, Header:=xlYes
    End With
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
And it's answered there. But to keep it available directly here I'm posting my solution (on excelforum there is also a second one) here:

VBA Code:
Sub Sort_ByType()
Dim Lr As Long

' Set the custom order list
Application.AddCustomList ListArray:=Sheets("Unique").Range("Z1:Z5")

With Sheets("Account Numbers")
  Lr = .Cells(.Rows.Count, "C").End(xlUp).Row
  .Range("A1:C" & Lr).Sort Key1:=.Range("C2"), Order1:=xlAscending, _
    OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, Header:=xlYes
End With

' clean up
Application.DeleteCustomList Application.CustomListCount
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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