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:
The recorded macro:
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?
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: