Custom sort using named ranges

jwarren73

New Member
Joined
Jan 22, 2015
Messages
37
Hello - I'm having trouble developing a custom sort code that uses values in a named range for the sort options, and was hoping that someone could point out what I overlooked - I just started using sort codes yesterday, and the Range.Sort syntax provided by Microsoft is less than helpful..

I'm using the following code to copy and sort data after new entries are added through a userform:
Code:
    Sheet5.Range("L2:L300").Copy
    With Sheet5
        .Range("R2").PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
        .Range("S2:S300").Sort Key1:=.Range("S2"), Order1:=xlAscending
    End With

That code works perfectly for basic sorting. However, I'm also trying to re-sort a related group of data that is organized by a custom sort - the following code does not work:
Code:
Sheet5.Range("L2:R300").Sort Key1:=Sheet5.Range("Q2"), Order1:=xlAscending, CustomOrder:=MasterPrimaryLocation, Key2:=Sheet5.Range("R2"), Order2:=xlAscending, CustomOrder:=Shift, Key3:=Sheet5.Range("P2"), Order3:=xlAscending, CustomOrder:=MasterPosition, Key4:=Sheet5.Range("L2"), Order4:=xlAscending

Named ranges are MasterPrimaryLocation, Shift, and MasterPosition. These ranges are set dynamically, with the intent of removing any need to adjust code as options in those ranges will change over time. Using quotations around the range names doesn't work as it looks for those names to actually sort with, and removing the quotations appears to cause issues as those names are not defined within the VBA - I'm not sure how to tell VBA to look for that information in the Name Manager.

Any suggestions?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hmmm

Changing CustomOrder:=Shift to CustomOrder:="AM,PM" results in a Named Argument not found error... so I must be missing something else....
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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