Question

Can anyone please help as I'm new to Excel VBA?

My requirement is to change colour of a range of cell based on value in a particular cell.

e.g. if I've cell B8 with value 2 then change the background colour of B9:B11 to red and C9:C11 to red.

and say if the cell B8 has value 3 then change the background colour of B9:B11 to red, C9:C11 to red and then D9:D11 red.

The range expands to B9:E11 for value 4 and so on. So need a VBA code, Conditional formating for 100 value will not be time effective I guess.

How do I do this?

Thanks Amrik

Was it helpful?

Solution

Use conditional formatting. Select B9:B11, make a conditional formatting rule based upon formula (=$B$8 >= COLUMN(B$9). Copy B9:B11 to C9:C11, etc and the conditional formatting rule will follow.

If you prefer to be more explicit, you could further avoid vba and just use propel (http://propel.codeplex.com). This example does what I think you described. Just hide rows 1 and 2 when you're done.

OTHER TIPS

Use a worksheet event handler to catch when the value in the cell changes. And using the code below you'll be able to change the color of the range

Private Sub worksheet_Change(ByVal target As Range)
    Range(Cells(9, 2), Cells(11, Cells(2, 8))).Interior.Color = vbRed
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top