Dynamic Range

zinah

Active Member
Joined
Nov 28, 2018
Messages
368
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a table that it's macro driven. What I need is to sort this table based on criteria and I have recorded below macro to do that, however, the range is dynamic and it depends on number employees that are reporting to each manager.
What I need is how to write a macro that can sort according to the dynamic range?

The dynamic range macro is as below:

Code:
Dim rSht As WorksheetSet rSht = Sheets("Role Scorecard")


Dim j As Integer
    j = rSht.[V13].Row
rSht.[U12].Value = "V12:Z" & j

The recorded macro:

Code:
Sub Macro1()

    Range("V14:Z14").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "X14:X67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "Y14:Y67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "Z14:Z67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "W14:W67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Role Scorecard").Sort
        .SetRange Range("V13:Z67")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

How can I make all the end range, i.e. in this recorded example "67" dynamic "X14:X67" knowing that all ranges starts from 14?

Code:
:=Range( _
        "X14:X67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "Y14:Y67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "Z14:Z67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "W14:W67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
If you first code creates the variable "j" then use "j" in the second code

Code:
ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:= _
        Range("X14:X" & j), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "Y14:Y" & j), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "Z14:Z" & j), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "W14:W" & j), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
 
Upvote 0
Thank you Michael I tried your way and it worked, I also tried also another way and it also worked and like to share it with you:

Code:
Range("V14:Z" & Range("V" & Rows.Count).End(xlUp).Row).Select     Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "X14:X" & Rows.Count), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "Y14:Y" & Rows.Count), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "Z14:Z" & Rows.Count), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Role Scorecard").Sort.SortFields.Add Key:=Range( _
        "W14:W" & Rows.Count), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
 
Upvote 0
Thanks for sharing......Doesn't really matter how you do it .....if it works for you...:beerchug:
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
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