RockandGrohl
Well-known Member
- Joined
- Aug 1, 2018
- Messages
- 801
- Office Version
- 365
- Platform
- Windows
Hi guys, believe it or not, this is my first time applying conditional formatting via VBA.
It looks a bit whacky, but I have four columns equally spaced apart and I want to apply four separate colour scale conditional formats to them (red = low, green = high)
Using the macro recorder gave me a bit of a mess, and I've cleaned it up a bit but am wondering if it can be made better?
Thanks!
It looks a bit whacky, but I have four columns equally spaced apart and I want to apply four separate colour scale conditional formats to them (red = low, green = high)
Using the macro recorder gave me a bit of a mess, and I've cleaned it up a bit but am wondering if it can be made better?
VBA Code:
' Conditional Formatting Rule
Cells.FormatConditions.Delete
Range("Q4").Activate
Dim cfrng As Range
Set cfrng = Range(Cells(4, ActiveCell.Column + 0), Cells(Lastrow + 1, ActiveCell.Column + 0))
x = 1
For x = 1 To 4
With cfrng
.FormatConditions.AddColorScale ColorScaleType:=3
.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).ColorScaleCriteria(1).Type = xlConditionValueLowestValue
With cfrng.FormatConditions(1).ColorScaleCriteria(1).FormatColor
.Color = 7039480
.TintAndShade = 0
End With
.FormatConditions(1).ColorScaleCriteria(2).Type = xlConditionValuePercentile
.FormatConditions(1).ColorScaleCriteria(2).Value = 50
With cfrng.FormatConditions(1).ColorScaleCriteria(2).FormatColor
.Color = 8711167
.TintAndShade = 0
End With
.FormatConditions(1).ColorScaleCriteria(3).Type = xlConditionValueHighestValue
With cfrng.FormatConditions(1).ColorScaleCriteria(3).FormatColor
.Color = 8109667
.TintAndShade = 0
End With
ActiveCell.Offset(0, 11).Activate
Next x
Thanks!