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).Value <> Worksheets("Request Results").Cells(i, 6).Value Then
Cells(i, 1).EnitreRow.Interior.ColorIndex = 255
ElseIf Worksheets("Request Results").Cells(i, 4).Value = Worksheets("Request Results").Cells(i, 6).Value Then
Cells(i, 1).EntireRow.Interior.ColorIndex = 5296274
End If
Next i
The loop goes into the else statement first and I get
"Subscript out of range"
on
Cells(i, 1).EntireRow.Interior.ColorIndex = 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:
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!
将颜色索引更改为颜色。
Cells(i, 1).EntireRow.Interior.Color = 5296274
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
您在第一个If语句中拼写了整个
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.