pawlactb
pawlactb

Reputation: 109

Why does VBA fail to sort this range?

I have the following range (obviously not mine, but MWE):

excel range description

And I am looking to sort it using VBA. here is my code:

Sub test()
    LR = ActiveWorkbook.Sheets(1).UsedRange.Rows.Count + ActiveSheet.UsedRange.Rows(1).Row - 1

    Dim ws As Worksheet
    Dim sort_range As Range

    Set ws = ActiveWorksheet.Worksheets(1)
    Set sort_range = ws.Range("C$2$:$E$" & LR)

    Call sort_range.Sort(Key1:=sl_ws.Range("$C$2"), Order1:=xlAscending, _
                            Key2:=sl_ws.Range("$D$2"), Order2:=xlAscending, _
                            Key3:=sl_ws.Range("$E$2"), Order3:=xlAscending, _
                            Header:=xlYes)
End Sub

This returns that the following error:

error,

directing me to this line:

    Set sort_range = ws.Range("C$2$:$E$" & LR)

What am I doing wrong? Any help is appreciated.

Upvotes: 0

Views: 68

Answers (1)

Michal
Michal

Reputation: 5750

Range("C$2$:$E$" & LR) - you have the dollar signs in wrong places - should be Range("$C$2:$E$" & LR).

Upvotes: 4

Related Questions