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.
In the VBA Editor, from the Tools menu, select Options and then select the Editor Format tab. In the list of Code Colors, select 'Normal Text'. From the Foreground dropdown list, select 'Yellow'. From the Background dropdown list select 'Black'.
You can use either:
ActiveCell.Interior.ColorIndex = 28
or
ActiveCell.Interior.Color = RGB(255,0,0)
This is a perfect example of where you should use the macro recorder. Turn on the recorder and set the color of the cells through the UI. Stop the recorder and review the macro. It will generate a bunch of extraneous code, but it will also show you syntax that works for what you are trying to accomplish. Strip out what you don't need and modify (if you need to) what's left.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With