Macro for custom sort

psrs0810

Well-known Member
Joined
Apr 14, 2009
Messages
1,109
can any help with a custom sort in a macro.

I recorded the one below, but it is not sorting and I need to change the range to be dynamic based on the info in column C starting in row 4

Application.AddCustomList ListArray:=Array("Medicare Subtotal", " Other Medicare HMO Subtotal”,”Senior Blue Subtotal”,”Medical Assistance Subtotal”,”Amerihealth Mercy Subtotal”,”CCBH Subtotal”,”Gateway Subtotal”,”MedPlus Three Rivers Subtotal”,”Aetna Subtotal”,”Amerihealth Admin Subtotal”,”Auto Subtotal”,”BHP Subtotal”,”BHP Employee Subtotal”,”Blue Shield Subtotal”,”Capital Blue Cross Subtotal”,”Cigna Subtotal”,”Health America Subtotal”,”Independence BC Subtotal”,”Keystone Central Subtotal”,”Keystone East Subtotal”,”Self-Pay Subtotal”,”United Healthcare Subtotal”,”Workers Comp Subtotal", _<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
"Senior Blue")
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1:A140") _
, SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"Medicare,Medicare HMO,Senior Blue", DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:AL140")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try:

Code:
    Dim LR As Long
    With ActiveWorkbook.Worksheets("Sheet2")
        LR = .Range("C" & .Rows.Count).End(xlUp).Row
    End With
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("C3:C" & LR) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
        "Medicare Subtotal, Other Medicare HMO Subtotal,Senior Blue Subtotal,Medical Assistance Subtotal,Amerihealth Mercy Subtotal,CCBH Subtotal,Gateway Subtotal,MedPlus Three Rivers Subtotal,Aetna Subtotal,Amerihealth Admin Subtotal,Auto Subtotal,BHP Subtotal,BHP Employee Subtotal,Blue Shield Subtotal,Capital Blue Cross Subtotal,Cigna Subtotal,Health America Subtotal,Independence BC Subtotal,Keystone Central Subtotal,Keystone East Subtotal,Self-Pay Subtotal,United Healthcare Subtotal,Workers Comp Subtotal,Senior Blue" _
        , DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet2").Sort
        .SetRange Range("A3:O" & LR)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Upvote 0
I don't get it. It works when I sort manually and when I recorded it and ran the recorded verson. As soon as the range is changed to dynamic, it does not work
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,510
Members
452,918
Latest member
Davion615

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