Sort not working in a macro

colinharwood

Active Member
Joined
Jul 27, 2002
Messages
426
Office Version
  1. 2019
Platform
  1. Windows
Hi
The following code has recently stopped working.
Just wondering if anyone can see why.
It falls over at the line
.Apply

Thanks
Colin

VBA Code:
Sub SortAndAddBorders()
'
' Add new member, sort & add borders to members list
'
    Dim LastRow As Integer
    LastRow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
    Range("B6:AZ" & LastRow).Select
    ActiveWorkbook.Worksheets("TMES Members").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("TMES Members").Sort.SortFields.Add2 Key:=Range( _
        "B6:B100"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("TMES Members").Sort
        .SetRange Range("B6:AZ99")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("A6").Select
    AddNumbers
    AddBorders
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
This is a screenshot of the error message when it falls over

Screenshot_1.jpg
 
Upvote 0
Changed the code to
VBA Code:
 lr = Cells(Rows.Count, 1).End(xlUp).Row
     Range("B5:AF" & lr).Select
    Selection.AutoFilter
          ActiveWorkbook.Worksheets("TMES Members").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("TMES Members").AutoFilter.Sort.SortFields.Add2 Key _
        :=Range("B5:B" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("TMES Members").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Selection.AutoFilter
        
    AddNumbers
    AddBorders

and it now works as expected
 
Upvote 0
Solution

Forum statistics

Threads
1,214,975
Messages
6,122,537
Members
449,088
Latest member
RandomExceller01

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