Hello Excel experts,
I'm struggling with adding something like conditional formatting in VBA: the lower is the number, the greener must be the cell (like built-in Excel conditional formatting).
So far I was able to find and modify the following VBA code, but it crashes on the following line, plus the scale is 2-color, not 3 color, like originally in Excel.
Do you know how can I do it?
Thank you very much for any suggestions in advance!
I'm struggling with adding something like conditional formatting in VBA: the lower is the number, the greener must be the cell (like built-in Excel conditional formatting).
So far I was able to find and modify the following VBA code, but it crashes on the following line, plus the scale is 2-color, not 3 color, like originally in Excel.
VBA Code:
cell.Interior.Color = RGB(255, 255 - colorValue, 255 - colorValue)
Do you know how can I do it?
Thank you very much for any suggestions in advance!
VBA Code:
Sub UpdateConditionalFormatting(rng As Range)
Dim cell As Range
Dim colorValue As Integer
Dim min As Integer
Dim avrg As Integer
Dim max As Integer
min = WorksheetFunction.min(rng)
max = WorksheetFunction.max(rng)
avrg = WorksheetFunction.Average(rng)
For Each cell In rng.Cells
If (cell.Value <= avrg) Then
colorValue = (cell.Value / max) * 255
cell.Interior.Color = RGB(255 - colorValue, 255, 255 - colorValue)
Else
colorValue = (cell.Value / min) * 255
cell.Interior.Color = RGB(255, 255 - colorValue, 255 - colorValue)
End If
Next cell
End
End Sub