Sorting via CustomOrder

kalim

Board Regular
Joined
Nov 17, 2010
Messages
87
Hi excel users.

I have vba code that sorts a table via 3 criteria (Year, Gender and last name). The problem is I am getting a 'compile error: defined name not found' message.
It seems not to like the CustomOrder1 code (causing the error).
The code works if I use the macro recorder to create the code, but when I tried to clean it up, I can't seem to quite get it to work.

What am I missing?
Thanks.

Code:
Sub sort()
With Range("table2")
 
   .Sort Key1:=Range("Table2[Year]"), Order1:=xlAscending, CustomOrder1:="K,1,2", _
    Key2:=Range("Table2[Gender]"), Order2:=xlAscending, _
    Key3:=Range("Table2[Last name]"), Order3:=xlAscending, _
    MatchCase:=False, Orientation:=xlTopToBottom, Header:=xlYes
 
    End With
 
 
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
There isn't a CustomOrder1 argument. There is an OrderCustom argument which accepts a one-based integer offset to the list of custom sort orders.
 
Upvote 0
Hi Andrew thanks for the reply.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Ok if there is no CustomOrder argument then why is it allowed (works) if you record the macro with the macro recorder?<o:p></o:p>
Surly it must be possible then?
<o:p></o:p>
Thanks.<o:p></o:p>
 
Upvote 0
Hi again Andrew, but I just checked again and the macro recorder does write the code using CustomOrder (excel 2007 and 2010) - maybe you are using an earler version of excel???

As I said before I cut most of it down, just not sure how to add the CustomOrder part of it.

Here is the exact code the recorder produces.

Code:
Sub Macro1()
'
' Macro1 Macro
'
'
    ActiveWorkbook.Worksheets("One").ListObjects("Table2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("One").ListObjects("Table2").Sort.SortFields.Add Key _
        :=Range("Table2[Year]"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        CustomOrder:="K,1,2", DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("One").ListObjects("Table2").Sort.SortFields.Add Key _
        :=Range("Table2[Gender]"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("One").ListObjects("Table2").Sort.SortFields.Add Key _
        :=Range("Table2[Last name]"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("One").ListObjects("Table2").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Thanks.
 
Upvote 0
It uses CustomOrder with the Add method of the SortFields object. It doesn't use it with the Sort method of the Range object - that's OrderCustom. What's recorded has changed in Excel 2007, which uses the new Sort object. Here's what I get in Excel 2003:

Code:
    Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=2, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
 
Upvote 0
You are absolutely right Andrew thanks.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
I got it to work for me.<o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,224,525
Messages
6,179,319
Members
452,905
Latest member
deadwings

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