Maths student G
Maths student G

Reputation: 15

VBA Color Cells Depending on the value

I would like to color cells depending on their value. If the value is 0, the cell should be green, if 1 then red. The whole thing should be a progression so the closer the value is to 1 the redder the cell should be.

Is there a suitable function for this?

Many thanks:)

My current solution looks like that but its not very pretty;)

Cells(i, 20) = (l / j) * 1 
        
Cells(i, 20).Interior.Color = RGB(255 * (l / j), 255 * (1 - (l / j)), 0)

Upvotes: 0

Views: 54

Answers (1)

SJR
SJR

Reputation: 23081

Think it makes more sense to use conditional formatting for this, but the code below produces the results in the picture.

Sub x()

Dim r As Range

For Each r In Range("A1:A11")
    r.Interior.Color = RGB(255 * r.Value, 255 * (1 - r.Value), 0)
Next r

End Sub

enter image description here

Upvotes: 1

Related Questions