Sorting table gives error

eirikdaude

Board Regular
Joined
Nov 26, 2013
Messages
93
Office Version
  1. 365
Platform
  1. Windows
I'm having a bit of trouble with sorting a table using VBA. The macro I'm using is this

VBA Code:
Private Sub test()
    Dim lo As ListObject
    
    Set lo = HCP_justering.ListObjects(1)
    
    lo.AutoFilter.ShowAllData
    
    With lo.Sort
        .SortFields.Clear
        .SortFields.Add _
            Key:=lo.ListColumn(1).Range, _
            SortOn:=xlSortOnValues, _
            Order:=xlAscending, _
            DataOption:=xlSortNormal
        .Header = xlYes
        .MatchCase = True
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

However I get an error on the line ".SortFields.Add (...)" saying "Runtime error 438. Object doesn't support this property or method".

Can someone give me a pointer to where I'm going wrong?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
It should be ListColumns(1), your missing the s
 
Upvote 0
Solution
Ugh, after having struggled with this for ages, I of course see the problem five minutes after posting here. It should be

VBA Code:
Key:=lo.ListColumns(1).Range, _

not

VBA Code:
Key:=lo.ListColumn(1).Range, _
 
Upvote 0
Try sorting like this

VBA Code:
Sub jec()
  With Sheets(1).ListObjects(1).DataBodyRange
    .Sort .Cells(1, 1), 1, , , , , , 1
  End With
End Sub
 
Upvote 0
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,216,102
Messages
6,128,852
Members
449,471
Latest member
lachbee

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