Color Entire Row Based on Cell Value Color Entire Row Based on Cell Value vba vba

Color Entire Row Based on Cell Value


change ColorIndex to Color.

Cells(i, 1).EntireRow.Interior.Color = 5296274


Does anyone know what could be causing this error?

From the MSDN help on the ColorIndex Property:

The ColorIndex property can have valid integer arguments between 0 and 56 that generate color. However, you can assign decimal or string values to the property without generating a run-time error. In these instances, Excel tries to randomly apply a color that corresponds to the argument value. However, setting the property to an integer value outside the range of 0 to 56 causes the following error:

Runtime Error '9': Subscript out of range

You can find the Color Palette with the valid indices on the same page:

enter image description here

Note that ColorIndex is different than Color, which uses the RGB specification and is more versatile. More info on Color vs ColorIndex here.

I personally prefer using Color and the built-in VBA Color Enumerations vbBlack, vbRed, vbGreen, vbYellow, vbBlue, vbMagenta, vbCyan, and vbWhite. For most applications these are enough, but if more colors are necessary then using a custom enumeration color is also possible, and more elegant than looking up the RGB tables..

I hope this helps!


I used something like this on a similar row highlighting problem.

For Each Rng in SomeRange.Columns(1).Cells  Rng.EntireRow.Interior.Color = 5296274Next


matomo