Change range in a sort macro

ERed1

Board Regular
Joined
Jun 26, 2019
Messages
104
Hi all,

I am working on this sheet and I need a lot of data to be sorted in a specific manner. The only issue is that the data I use changes. One week I may have 5000 rows of data in Excel, and others I may have 6000. Is there a way to make the sorta range in this code here change based on how many rows there are?
Code:
    Cells.Select    ActiveWorkbook.Worksheets("REPORT").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("REPORT").Sort.SortFields.Add2 Key:=Range( _
        "A1:AW7000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("REPORT").Sort.SortFields.Add2 Key:=Range( _
        "A1:AW7000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("REPORT").Sort.SortFields.Add2 Key:=Range( _
        "A1:AW7000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("REPORT").Sort
        .SetRange Range("A1:AW7000")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

In the code above I tried changing all of the "A1:AW7000" to "Cells.Select" but that didn't work. All help is appreciated, thanks!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Do you really need sort every cell in the worksheet or is it every cell in the used range? You could try using a range object like
Code:
Dim My_Range as range
set MY_Range= activesheet.usedrange

and then use that in place of your range
 
Upvote 0
Hi all,

I am working on this sheet and I need a lot of data to be sorted in a specific manner. The only issue is that the data I use changes. One week I may have 5000 rows of data in Excel, and others I may have 6000. Is there a way to make the sorta range in this code here change based on how many rows there are?
Code:
    Cells.Select    ActiveWorkbook.Worksheets("REPORT").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("REPORT").Sort.SortFields.Add2 [COLOR=#ff0000]Key:=Range( _
        "A1:AW7000")[/COLOR], SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("REPORT").Sort.SortFields.Add2 [COLOR=#ff0000]Key:=Range( _
        "A1:AW7000")[/COLOR], SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("REPORT").Sort.SortFields.Add2[COLOR=#ff0000] Key:=Range( _
        "A1:AW7000")[/COLOR], SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("REPORT").Sort
        .SetRange Range("A1:AW7000")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

In the code above I tried changing all of the "A1:AW7000" to "Cells.Select" but that didn't work. All help is appreciated, thanks!


In your code you have the same range in each key to order.
So you don't need 3 keys, it can be like this:

Code:
Sub test()
  Range("A1:AW" & Range("A" & Rows.Count).End(xlUp).Row).Sort key1:=Range("A1"), order1:=xlAscending, Header:=xlYes
End Sub
 
Last edited:
Upvote 0
In your code you have the same range in each key to order.
So you don't need 3 keys, it can be like this:

Code:
Sub test()
  Range("A1:AW" & Range("A" & Rows.Count).End(xlUp).Row).Sort key1:=Range("A1"), order1:=xlAscending, Header:=xlYes
End Sub
Thank you. I got this to work perfectly.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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