Sort code no longer works

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Hi,
Ive moved a code from one worksheet to another,code supplied.
Ive names things the same so not sure why no it doesnt work and i get a RTE9 out of range.
The code in question is shown in yellow below.
Can you advise where i shouold be looking to correct this please.


Rich (BB code):
Private Sub SortList_Click()
    Dim oLo As ListObject
    Dim oLc1 As ListColumn, oLc2 As ListColumn
    Dim i As Integer, x As Integer
    Dim splitThis As String, alpha As String, num As String
    
Application.ScreenUpdating = False
'setup what/where to workwith
Set oLo = ActiveWorkbook.Worksheets("INFO").ListObjects("Table20")
'add 2 columns "Alpha" and "Num"
With oLo
    Set oLc1 = .ListColumns.Add
        oLc1.Name = "Alpha"
    Set oLc2 = .ListColumns.Add
        oLc2.Name = "Num"
    'populate the new columns
    For x = 1 To oLo.DataBodyRange.Rows.Count
        splitThis = .ListColumns("SORT").DataBodyRange.Rows(x)
        alpha = "": num = ""
        For i = 1 To Len(splitThis)
            If IsNumeric(Mid(splitThis, i, 1)) Then
                num = num & Mid(splitThis, i, 1)
            Else
                alpha = alpha & Mid(splitThis, i, 1)
            End If
        Next i
        .ListColumns("Alpha").DataBodyRange.Rows(x) = alpha
        .ListColumns("Num").DataBodyRange.Rows(x) = num
    Next x
    'sort
    With .Sort
        .SortFields.Clear
        .SortFields.Add _
            Key:=Range("Table20[Alpha]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal
        .SortFields.Add _
            Key:=Range("Table20[Num]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    'remove the 2 added columns
    .ListColumns("Num").Delete
    .ListColumns("Alpha").Delete
End With
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
At a guess, I would say it has to do with the unqualified ranges used as sort keys.

VBA Code:
        .SortFields.Add _
            Key:=Range("Table20[Alpha]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal
        .SortFields.Add _
            Key:=Range("Table20[Num]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal


Better if you were to explicitly identify them:
VBA Code:
        .SortFields.Add Key:=ActiveWorkbook.Worksheets("INFO").Range("Table20[Alpha]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SortFields.Add Key:=ActiveWorkbook.Worksheets("INFO").Range("Table20[Num]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
 
Upvote 0
Can you advise where i shouold be looking to correct this please.
Verify that the table name and the column name in the code match what you have on the sheet.
 
Upvote 0
Morning,

In the code was SORT but column name was I CODE SORT.
Now when i run the code it operates without any error message.

Many thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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