Sort muliple columns by named or dynamic range

Magic Man

New Member
Joined
Oct 20, 2015
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Hi All,

I have a macro that will sort rows 8 to 57 by columns D, E, and F

Code:
Sub Sortcolums()
Dim rCount As Long
rCount = Evaluate("COUNT(" & ActiveSheet.Name & "!D8:D57)")
With ActiveSheet.Sort
    With .SortFields
        .Clear
        .Add Key:=Range("D8").Resize(rCount), Sorton:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        .Add Key:=Range("E8").Resize(rCount), Sorton:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        .Add Key:=Range("F8").Resize(rCount), Sorton:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    End With
    .SetRange Range("C7:AP7").Resize(rCount + 1)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
End Sub

However, what I want to do is change the fixed range to a dynamic range. This is so when I add rows after row 57 I don't have to go back in and re-edit this macro. Nothing above row 8 is to be included in the sort.

I'd like this macro to be used on the activesheet as opposed to a named sheet.

Open to all and any ideas and thanks.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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