0
votes

I'm working in excel on programatically setting a cell's background color to be a specific RGB color that is referenced in nearby cells. Here's what I'm trying to do:

enter image description here

You can see that I manually set the background color to be the RGB values specified in the cells in the same row on the left, as highlighted in the picture. The first purple cell has RGB (148,91,128).

I want to do this in VBA or maybe even conditional formatting if possible. So far I have tried this simple vba line of code to see if it would work:

Range("F1").Interior.Color = RGB(C1, D1, E1)

However this line of code sets the cell to have a black background like this:

enter image description here

If the code had worked correctly, this cell should have been a nearly white color, not black. Any ideas on why this isn't working? Am I making a mistake is the way I reference the cells for RGB?

It would be nice to be able to assign the range "F1:F__" to reference the cells to the left on each row too, not just one cell. Thanks!

1

1 Answers

1
votes

Try this:

Range("F1").Interior.Color = RGB(Range("C1"), Range("D1"), Range("E1"))

In your version of code C1, D1, E1 are taken as variables and they have default value 0, so macro assign color equal to RGB(0, 0, 0) which is black.