Color Entire Row Based on Cell Value

前端 未结 3 1481
小鲜肉
小鲜肉 2020-12-17 16:50

I\'m trying to color an entire row if two cells in that row have the same value.

For i = 2 To LastRow
    If Worksheets(\"Request Results\").Cells(i, 4).Valu         


        
相关标签:
3条回答
  • 2020-12-17 17:09

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

    For Each Rng in SomeRange.Columns(1).Cells
      Rng.EntireRow.Interior.Color = 5296274
    Next
    
    0 讨论(0)
  • 2020-12-17 17:27

    change ColorIndex to Color.

    Cells(i, 1).EntireRow.Interior.Color = 5296274
    
    0 讨论(0)
  • 2020-12-17 17:33

    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!

    0 讨论(0)
提交回复
热议问题