Trying to sort a ListObject Table using a custom order range

rtemen

New Member
Joined
Sep 23, 2013
Messages
36
Hi.
I have a sheet with a table. I am trying to sort table 'DeviceSummary' using Column 'ID'

Table DeviceSummary:
ID Col2 Col3 Value
1,1 A B 12
1,2 C D 15

etc

I need to sort the table using column ID.
I set up a range defining the criteria to sort on.

Here is my code: (This is pulled from a forum where the user thanked the people that gave this to him saying that it works great.)
I have tweaked things til I am going nuts, and cannot get anywhere.

Code:
    ActiveWorkbook.Worksheets("Summary").ListObjects("DeviceSummary").Sort.SortFields.Clear
    
    On Error Resume Next
    Application.AddCustomList listArray:=Worksheets("Rates").Range("CustomList")  ' My list of sorting instructions
    On Error GoTo 0
    
    
    With ActiveWorkbook.Worksheets("Summary").ListObjects("DeviceSummary").Sort
        .SortFields.Clear
        .SortFields.Add _
            key:=.ListColumns(ID), _
            SortOn:=xlSortOnValues, _
            Order:=xlAscending, _
            CustomOrder:=Worksheets("Rates").Range("CustomList"), _
            DataOption:=xlSortNormal
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

No matter what I have tried, I keep getting a Run-Time error '438': Object doesn't support this property or method.

Any help is appreciated.
Rich
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Rich

Where do you get the error?

Also, have you checked the custom list has been created/added?
 
Upvote 0
Rich

Where do you get the error?

Also, have you checked the custom list has been created/added?



I apparently do not know how to respond properly on this forum.
Hope this is right.

The error comes on the line .sortfields.add.
Yes, the custom list is there and works.

I first set up a search using the custom list on a test workbook only using it on data range in a sheet.

Now, I am trying to implement it on this workbook where the data is actually in a table, hence the ListObject stuff.
But I cannot seem to get all the parts right.

Rich
 
Upvote 0
Rich

At first look I think this is the problem.
Code:
Key:=.ListColumns(ID)
.ListColumns is not a property of the Sort object of a ListObject, in this case not a property of ActiveWorkbook.Worksheets("Summary").ListObjects("DeviceSummary").Sort.

What are you trying to set the Key to?
 
Upvote 0
Assuming the custom list is a columnar range, you could use this without adding the custom list:

Code:
    Dim lo as listobject
set lo = ActiveWorkbook.Worksheets("Summary").ListObjects("DeviceSummary")
    
    
    With lo.Sort
        .SortFields.Clear
        .SortFields.Add _
            key:=lo.ListColumns(ID), _
            SortOn:=xlSortOnValues, _
            Order:=xlAscending, _
            CustomOrder:=Join(application.Transpose(Worksheets("Rates").Range("CustomList")), ","), _
            DataOption:=xlSortNormal
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Upvote 0
Assuming the custom list is a columnar range, you could use this without adding the custom list:

Code:
    Dim lo as listobject
set lo = ActiveWorkbook.Worksheets("Summary").ListObjects("DeviceSummary")
    
    
    With lo.Sort
        .SortFields.Clear
        .SortFields.Add _
            key:=lo.ListColumns(ID), _
            SortOn:=xlSortOnValues, _
            Order:=xlAscending, _
            CustomOrder:=Join(application.Transpose(Worksheets("Rates").Range("CustomList")), ","), _
            DataOption:=xlSortNormal
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


The SortFields.add line gives me a Run-time error 13: Type Mismatch.

Also, the column that makes up my Custom List is a column of strings. The strings contain a comma.
Like so:
1,1
1,2
1,3
1,4
1,5
etc.

So we cannot build the list using your Join.
Interestingly, this function was working on my other spreadsheet where I just had a range of cells, rather than an actual table.
But, on this workbook, I cannot get this to work on the Table.

Rich
 
Upvote 0
Rich

The problem with the code in your original post was this.
Code:
key:=.ListColumns(ID)
So what are you trying to use for the Key argument?

If it's a column within the table you'll need to fully qualify ListColumns and use Range.

So, stealing a little from Rory's code, try this.
Code:
Dim lo As ListObject

    Set lo = ActiveWorkbook.Worksheets("Summary").ListObjects("DeviceSummary")

    On Error Resume Next
    Application.AddCustomList listArray:=Worksheets("Rates").Range("CustomList")  ' My list of sorting instructions
    On Error GoTo 0

    With lo.Sort
        .SortFields.Clear
        .SortFields.Add _
                Key:=lo.ListColumns(ID).Range, _
                SortOn:=xlSortOnValues, _
                Order:=xlAscending, _
                CustomOrder:=Application.CustomListCount, _
                DataOption:=xlSortNormal
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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