I am trying to apply conditional formatting to a range of cells, whereby formatting is only applied to those rows with an "x" mark in a column, and second only for a range of columns (e.g. rg2)
I have tried the following code, but haven't been able to combine the column and row selection.
I would highly appreciate anyone's help!
I have tried the following code, but haven't been able to combine the column and row selection.
I would highly appreciate anyone's help!
Code:
Private Sub ShowMain_Click()
Dim cs As ColorScale
StartR1 = 4
StartR = 5
StartC = 7
Application.ScreenUpdating = False
Range(Cells(StartR, StartC), Cells(10000, StartC).End(xlUp)).EntireRow.Hidden = False
Size = Range(Cells(StartR, StartC), Cells(StartR, StartC).End(xlDown)).Rows.Count
Range(Cells(StartR, StartC), Cells(StartR, StartC).End(xlDown)).EntireRow.Hidden = True
For Each c In Range(Cells(StartR, 2), Cells(StartR + Size - 1, 2))
If c = "x" Then Rows(c.Row).Hidden = False
Next c
Range(Cells(StartR, StartC), Cells(10000, StartC).End(xlUp)).EntireRow.FormatConditions.Delete
SizeC1 = Range(Cells(StartR1, 1), Cells(StartR1, 1).End(xlToRight)).Columns.Count
SizeC2 = Range(Cells(StartR1, SizeC1), Cells(StartR1, SizeC1).End(xlToRight)).Columns.Count
Set rg2 = Range(Cells(StartR, SizeC1 + 2), Cells(Size - 1, SizeC2 + 2))
'colour scale will have three colours
Set rgc = Range("For Each c In Range(Cells(StartR, 2), Cells(StartR + Size - 1, 2))
If c = "x"
Next c")
Set cs = rgc.FormatConditions.AddColorScale(ColorScaleType:=3)
With cs
'First colour is red
With .ColorScaleCriteria(1)
.FormatColor.Color = RGB(255, 0, 0)
.Type = xlConditionValueNumber
.Value = -0.2
End With
'Second colour is yellow
With .ColorScaleCriteria(2)
.FormatColor.Color = RGB(255, 230, 153)
.Type = xlConditionValueNumber
.Value = 0
End With
'Third colour is Green
With .ColorScaleCriteria(3)
.FormatColor.Color = RGB(0, 176, 80)
.Type = xlConditionValueNumber
.Value = 0.2
End With
End With
Application.ScreenUpdating = True
End Sub