基于单元格值的VBA颜色整行

时间:2022-08-14 20:17:01

I'm trying to color an entire row if two cells in that row have the same value. Here's the code I have right now:

如果该行中的两个单元格具有相同的值,我正在尝试为整行着色。这是我现在的代码:

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 a "Subscript out of range" error on the Cells(i, 1).EntireRow.Interior.ColorIndex = 5296274 line and I'm not quite sure why. Does anyone know what could be causing this error? Everything I've tried has failed.

循环首先进入else语句,我在单元格(i,1)上得到“下标超出范围”错误.EntireRow.Interior.ColorIndex = 5296274行,我不太清楚为什么。有谁知道可能导致此错误的原因是什么?我尝试的一切都失败了。

2 个解决方案

#1


6  

change ColorIndex to Color.

将ColorIndex更改为Color。

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

#2


9  

Does anyone know what could be causing this error?

有谁知道可能导致此错误的原因是什么?

From the MSDN help on the ColorIndex Property:

从ColorIndex属性的MSDN帮助:

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:

ColorIndex属性可以具有0到56之间的有效整数参数,以生成颜色。但是,您可以为属性分配十进制或字符串值,而不会生成运行时错误。在这些情况下,Excel会尝试随机应用与参数值对应的颜色。但是,将属性设置为0到56范围之外的整数值会导致以下错误:

Runtime Error '9': Subscript out of range

运行时错误'9':下标超出范围

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

您可以在同一页面上找到带有效索引的调色板:

基于单元格值的VBA颜色整行

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

请注意,ColorIndex与Color不同,后者使用RGB规范,功能更多。有关Color vs ColorIndex的更多信息,请点击此处。

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..

我个人更喜欢使用Color和内置的VBA Color Enumerations vbBlack,vbRed,vbGreen,vbYellow,vbBlue,vbMagenta,vbCyan和vbWhite。对于大多数应用程序来说这些都足够了,但如果需要更多颜色,那么使用自定义枚举颜色也是可能的,并且比查找RGB表格更优雅。

I hope this helps!

我希望这有帮助!

#1


6  

change ColorIndex to Color.

将ColorIndex更改为Color。

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

#2


9  

Does anyone know what could be causing this error?

有谁知道可能导致此错误的原因是什么?

From the MSDN help on the ColorIndex Property:

从ColorIndex属性的MSDN帮助:

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:

ColorIndex属性可以具有0到56之间的有效整数参数,以生成颜色。但是,您可以为属性分配十进制或字符串值,而不会生成运行时错误。在这些情况下,Excel会尝试随机应用与参数值对应的颜色。但是,将属性设置为0到56范围之外的整数值会导致以下错误:

Runtime Error '9': Subscript out of range

运行时错误'9':下标超出范围

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

您可以在同一页面上找到带有效索引的调色板:

基于单元格值的VBA颜色整行

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

请注意,ColorIndex与Color不同,后者使用RGB规范,功能更多。有关Color vs ColorIndex的更多信息,请点击此处。

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..

我个人更喜欢使用Color和内置的VBA Color Enumerations vbBlack,vbRed,vbGreen,vbYellow,vbBlue,vbMagenta,vbCyan和vbWhite。对于大多数应用程序来说这些都足够了,但如果需要更多颜色,那么使用自定义枚举颜色也是可能的,并且比查找RGB表格更优雅。

I hope this helps!

我希望这有帮助!