Hi,
I have 20 groups of rows that I would like to sort by column G (cell colour) and column K (cell value).
The first group starts at row 19 (between G19:K42) and each group is spaced 6 rows apart (e.g. second group located between G49: K72).
I am trying to fit in a dynamic referencing/loop formula as shown below, but there are issues with the syntax. Would appreciate help with this.
Sub SortPM()
'
Dim i As Long
Dim currentrange As Range
Dim column1range As Range
Dim column2range As Range
For i = 0 To 19
Set currentrange = Range("G" & "(19 + i*30)", "K" & "(42 + i*30)") 'Does not work (range of object - 'Global' failed)
Set column1range = Range("G" & "(19 + i*30)", "G" & "(42 + i*30)")
Set column2range = Range("K" & "(19 + i*30)", "K" & "(42 + i*30)")
currentrange.Select
ActiveWorkbook.Worksheets("Priority map").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Priority map").Sort.SortFields.Add(column1range, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255 _
, 192, 0)
ActiveWorkbook.Worksheets("Priority map").Sort.SortFields.Add(column1range, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255 _
, 230, 153)
ActiveWorkbook.Worksheets("Priority map").Sort.SortFields.Add(column1range, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255 _
, 242, 204)
ActiveWorkbook.Worksheets("Priority map").Sort.SortFields.Add(column1range, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(84, _
150, 53)
ActiveWorkbook.Worksheets("Priority map").Sort.SortFields.Add Key:=Range( _
column2range), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Priority map").Sort
.SetRange currentrange
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Next i
End Sub
I have 20 groups of rows that I would like to sort by column G (cell colour) and column K (cell value).
The first group starts at row 19 (between G19:K42) and each group is spaced 6 rows apart (e.g. second group located between G49: K72).
I am trying to fit in a dynamic referencing/loop formula as shown below, but there are issues with the syntax. Would appreciate help with this.
Sub SortPM()
'
Dim i As Long
Dim currentrange As Range
Dim column1range As Range
Dim column2range As Range
For i = 0 To 19
Set currentrange = Range("G" & "(19 + i*30)", "K" & "(42 + i*30)") 'Does not work (range of object - 'Global' failed)
Set column1range = Range("G" & "(19 + i*30)", "G" & "(42 + i*30)")
Set column2range = Range("K" & "(19 + i*30)", "K" & "(42 + i*30)")
currentrange.Select
ActiveWorkbook.Worksheets("Priority map").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Priority map").Sort.SortFields.Add(column1range, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255 _
, 192, 0)
ActiveWorkbook.Worksheets("Priority map").Sort.SortFields.Add(column1range, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255 _
, 230, 153)
ActiveWorkbook.Worksheets("Priority map").Sort.SortFields.Add(column1range, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255 _
, 242, 204)
ActiveWorkbook.Worksheets("Priority map").Sort.SortFields.Add(column1range, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(84, _
150, 53)
ActiveWorkbook.Worksheets("Priority map").Sort.SortFields.Add Key:=Range( _
column2range), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Priority map").Sort
.SetRange currentrange
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Next i
End Sub