Sort a range dynamically

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
980
Hello All,

Below I have code that was created when I used the macro recorder. The only problem is that I want to apply that code to sort a different range each time I run it.
How can the code below (in red and bold) be modified to sort by the range that contains the bottom-most row of data in column A?



Code:
Sub aaa_test_macro_01()
 
    Cells.Select
    ActiveWorkbook.Worksheets("Totals").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Totals").Sort.SortFields.Add Key:= _
        Range("[B][SIZE=4][COLOR=red]A2:A460[/COLOR][/SIZE][/B]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Totals").Sort.SortFields.Add Key:= _
        Range("[SIZE=4][COLOR=red][B]B2:B460[/B][/COLOR][/SIZE]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Totals").Sort
        .SetRange Range("[SIZE=4][COLOR=red][B]A1:C460[/B][/COLOR][/SIZE]")
        .header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
 
End Sub


Thanks
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Try:

Code:
Sub aaa_test_macro_01()
    Dim LR As Long
    With ActiveWorkbook.Worksheets("Totals")
        LR = .Cells(.Rows.Count, 1).End(xlUp).Row
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:= _
            Range("A2:A" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        .Sort.SortFields.Add Key:= _
            Range("B2:B" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
                xlSortNormal
        With .Sort
            .SetRange Range("A1:C" & LR)
            .header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
End Sub
 

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
980
Andrew,

Believe it or not it took me this long to apply (my newbie fault :( ) to apply and implement that code. But now I understand it! :)

I LOVE dynamic code that has the ability to change everytime!

Worked perfectly!

Thank you much :biggrin:
 

Forum statistics

Threads
1,141,136
Messages
5,704,485
Members
421,353
Latest member
jekoxien15

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
Top