1

I know there are several threads on this topic but none of the answers have helped resolved this error.

I am trying to sort a table using three keys but receive the error, "Run-time error '1004': Sort method of Range class failed"

I've tried changing "Range("L2")" to ".Range("L2")" and received the error, "Compile error: Invalid or unqualified reference"

I've tried specifying the actual range instead of using columns, I've removed the last two keys and tried just the first, still received the run-time error.

shtData.Activate

shtData.Range(Range("A2"), Range("Z8000").End(xlUp)).Sort _
Key1:=Range("L2"), Order1:=xlAscending, _
Key2:=Range("M2"), Order2:=xlAscending, _
Key3:=Range("B2"), Order3:=xlAscending, _
Header:=xlYes

If you have any suggestions, I'd greatly appreciate it. I had this working yesterday and my excel crashed and did not recover the changes I made, I cannot figure out why I can't get it to work today.

7
  • Do you have headers in row 2? Commented Dec 1, 2016 at 18:10
  • @Rdster Yes, my headers are in row 2 Commented Dec 1, 2016 at 18:11
  • 1
    You need to fully qualify the ranges, which can provide errors if you don't. I.e. shtData.Range(shtData.Range("A2"), shtData.Range("Z8000").End(xlUp)).Sort, and the same with your Keys. Commented Dec 1, 2016 at 18:30
  • @BruceWayne, but that shtData.Activate makes it already Commented Dec 1, 2016 at 18:33
  • 1
    The dot is a member access. There's the global-scope Range which implicitly refers to the active sheet, and then there's a worksheet's .Range property, which refers to a range in that worksheet. A With block gives you an object to qualify members with - of course using an unqualified .Range is a compile error. Commented Dec 1, 2016 at 18:52

2 Answers 2

3

you most probably have no data in "Z" column

if data rows range can be safely sized by column "A" not empty cells then go like follows

Option Explicit

Sub main()
    Dim shtData As Worksheet

    Set shtData = Worksheets("Data") '<--| some setting of 'shtData'
    With shtData
        .Range("Z2", .Cells(.Rows.Count, "A").End(xlUp)).Sort _
        Key1:=.Range("L2"), Order1:=xlAscending, _
        Key2:=.Range("M2"), Order2:=xlAscending, _
        Key3:=.Range("B2"), Order3:=xlAscending, _
        Header:=xlYes
    End With
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Good call using With
@user3598756 Thank you! I completely forgot I removed a column yesterday so my data ends at Y now.
1

If it is possible you could end up changing amount of columns again in the future, you could do something like.

With shtData
Range(Range("A2"), Cells(Range("A8000").End(xlUp).Row, Range("ZZ2").End(xlLeft).Column)

That way it will automatically size the sort area for however many columns you are using as well as rows.

1 Comment

Awesome! Thanks for the advice. I am continually adjusting this data range to get my program to work so this will work much better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.