Sort data in multiple columns without a fixed range

j4ttlife

New Member
Joined
Feb 2, 2022
Messages
30
Platform
  1. Windows
  2. MacOS
Hi, I'm trying to use a macro that sorts data in multiple columns.

However, upon viewing the code after recording the macro, I notice a range has been declared (to row 483).

Issue with the below code is the ranges only go to row 483. Next week it may be 500+ etc


Original recorded Macro

VBA Code:
Cells.Select
    ActiveWorkbook.Worksheets("Oct data").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Oct Data").Sort.SortFields.Add2 Key _
        :=Range("A2:A483"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal

    ActiveWorkbook.Worksheets("Oct Data").Sort.SortFields.Add2 Key _
        :=Range("R2:R483"), SortOn:=xlSortOnValues, Order:=xlDescending, _
        DataOption:=xlSortNormal

    With ActiveWorkbook.Worksheets("Oct Data").Sort
        .SetRange Range("A1:AV483")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    Application.Run "ConnectChartEvents"
    Sheets("Oct Data").Select

My attempt below:

VBA Code:
Dim lastA As Long

    ActiveWorkbook.Worksheets("Oct Data").Sort.SortFields.Add2 Key _
        :=Range("A1:A" & lastA), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hey, can you check if the code below works for you?

VBA Code:
Dim lastRow As Long

With ActiveWorkbook.Worksheets("Oct data")
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    .Sort.SortFields.Clear
    .Sort.SortFields.Add2 Key:=.Range("A2:A" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add2 Key:=.Range("R2:R" & lastRow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal

    With .Sort
        .SetRange .Range("A1:AV" & lastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End With

Application.Run "ConnectChartEvents"
Sheets("Oct Data").Select
 
Upvote 0
How about
VBA Code:
   Dim UsdRws As Long
   
   With Worksheets("Oct data").Sort
      UsdRws = .Worksheet.Range("A" & Rows.Count).End(xlUp).Row
      .SortFields.Clear
      .SortFields.Add2 Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, _
         DataOption:=xlSortNormal
      .SortFields.Add2 Key:=Range("R1"), SortOn:=xlSortOnValues, Order:=xlDescending, _
         DataOption:=xlSortNormal
      
      .SetRange Range("A1:AV" & UsdRws)
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
   End With

    Application.Run "ConnectChartEvents"
    Sheets("Oct Data").Select
 
Upvote 0
Solution
How about
VBA Code:
   Dim UsdRws As Long
  
   With Worksheets("Oct data").Sort
      UsdRws = .Worksheet.Range("A" & Rows.Count).End(xlUp).Row
      .SortFields.Clear
      .SortFields.Add2 Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, _
         DataOption:=xlSortNormal
      .SortFields.Add2 Key:=Range("R1"), SortOn:=xlSortOnValues, Order:=xlDescending, _
         DataOption:=xlSortNormal
     
      .SetRange Range("A1:AV" & UsdRws)
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
   End With

    Application.Run "ConnectChartEvents"
    Sheets("Oct Data").Select
Thanks Fluff. I was receiving an error 438 but slightly ameneded the UsdRws variable range to the below and it has done the trick. Appreciate the help.

VBA Code:
UsdRws = Range("A" & Rows.Count).End(xlUp).Row
 
Upvote 0
Hey, can you check if the code below works for you?

VBA Code:
Dim lastRow As Long

With ActiveWorkbook.Worksheets("Oct data")
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    .Sort.SortFields.Clear
    .Sort.SortFields.Add2 Key:=.Range("A2:A" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add2 Key:=.Range("R2:R" & lastRow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal

    With .Sort
        .SetRange .Range("A1:AV" & lastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End With

Application.Run "ConnectChartEvents"
Sheets("Oct Data").Select
Thanks for the reply. I tried the other solution provided and have managed to resolve my issue, but I appreciate your help.
 
Upvote 0
I was receiving an error 438
Oops that line should have been
VBA Code:
      UsdRws = .Parent.Range("A" & Rows.Count).End(xlUp).Row
Your change may give the wrong value if the Oct Data sheet is not active.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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