Sort a range dynamically

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
1,027
Office Version
  1. 365
Platform
  1. Windows
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

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
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
 
Upvote 0
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:
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,924
Members
448,533
Latest member
thietbibeboiwasaco

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