Link to home
Start Free TrialLog in
Avatar of acdecal
acdecal

asked on

VBA Excel sort/ range Error with 2016

Since migrating from Excel 2010 to 2016, the code below now produces an error:  Method sort of object range failed.   Used to work fine.  Can someone please help with this?


Private Sub SortAll()

      Range(Range("TopDescription"), Range("TopDescription").End(xlDown)).EntireRow.Sort Key1:=Range("TopCostcode"), Order1:=xlAscending, Key2:=Range("TopPhase" _
        ), Order2:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:=False _
        , Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:= _
        xlSortNormal
       
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of acdecal
acdecal

ASKER

That works!  But now I'm getting the same error with this code line;

Range(Range("a1"), Range("a" & Rows.Count).End(xlUp)).EntireRow.Sort Key1:=Range("J1")
Please post your whole code to know the range, anyway try below:
Sub SortData()
Dim Ws As Worksheet
Dim LR As Long
Application.ScreenUpdating = False
Set Ws = ActiveSheet
LR = Ws.Range("A" & Rows.Count).End(xlUp).Row

Ws.Sort.SortFields.Clear
    Ws.Sort.SortFields.Add Key:=Range("J1:J" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With Ws.Sort
        .SetRange Range("A1:J" & LR)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Application.ScreenUpdating = True
End Sub

Open in new window

Avatar of acdecal

ASKER

OK Thanks!