views:

2929

answers:

4

As part of a VBA program, I have to set the background colors of certain cells to green, yellow or red, based on their values (basically a health monitor where green is okay, yellow is borderline and red is dangerous).

I know how to set the values of those cells, but how do I set the background color.

+2  A: 

You can use either:

ActiveCell.Interior.ColorIndex = 28

or

ActiveCell.Interior.Color = RGB(255,0,0)
Vinko Vrsalovic
dkusleika is right. Your second example should be ActiveCell.Interior.Color = RGB(255,0,0)
BradC
A: 

Duplicate of Multiple Cell Background Colors in Excel Spreadsheets...

PhiLho
PhiLho, that Q was about conditional formatting and had nothing to do with VBA programming, although I can see some of the answers mentioned VBA as the solution.
paxdiablo
@Pax: That's why I see it as duplicate (ie. you would have seen this solution if searching): linked thread provided both solutions, including the one given above. But I didn't closed your thread (partly because you could re-open it...) and because of the diff in goal... Beside, cross-ref is good.
PhiLho
+3  A: 

I need reputation to comment? Odd. Vinko's second example should be

ActiveCell.Interior.Color = RGB(255,0,0)

Dick Kusleika
A: 

or alternatively you could not bother coding for it and use the 'conditional formatting' function in Excel which will set the background colour and font colour based on cell value.

There are only two variables here so set the default to yellow and then overwrite when the value is greater than or less than your threshold values.