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
acdecalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ShumsDistinguished Expert - 2017Commented:
Hi,
Please let us know the name range TopDescription & TopPhase
Try below:
Sub SortData()
Dim Ws As Worksheet
Set Ws = ActiveSheet

Ws.Sort.SortFields.Clear
    Ws.Sort.SortFields.Add Key:=Range("TopDescription"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With Ws.Sort
        .SetRange Range("TopDescription")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
acdecalAuthor Commented:
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")
0
ShumsDistinguished Expert - 2017Commented:
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

1
acdecalAuthor Commented:
OK Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.