Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA基于其他列值为行着色_Vba_Excel - Fatal编程技术网

VBA基于其他列值为行着色

VBA基于其他列值为行着色,vba,excel,Vba,Excel,我试图根据列B、C、D的行颜色为列A中的每一行着色 假设A2颜色基于B2、C2、D2的颜色。如果其中一个有红色,则A2应为红色,否则A2为绿色 请在下面找到我的代码: 选项显式 分页1 暗淡的最后一样长 我想我会坚持多久 lastR=SheetsSheet1.CellsRows.Count,A.EndxlUp.Row 对于i=lastR到2步骤-1 如果SheetsSheet1.Cellsi,B或SheetsSheet1.Cellsi,C或SheetsSheet1.Cellsi,D=Rowsi.

我试图根据列B、C、D的行颜色为列A中的每一行着色

假设A2颜色基于B2、C2、D2的颜色。如果其中一个有红色,则A2应为红色,否则A2为绿色

请在下面找到我的代码:

选项显式 分页1 暗淡的最后一样长 我想我会坚持多久 lastR=SheetsSheet1.CellsRows.Count,A.EndxlUp.Row 对于i=lastR到2步骤-1 如果SheetsSheet1.Cellsi,B或SheetsSheet1.Cellsi,C或SheetsSheet1.Cellsi,D=Rowsi.Interior.Color=RGB255,0,0,则 Rowsi.Interior.Color=RGB0,255,0 如果结束 接下来我 端接头 我得到的下标超出范围,错误代码9

这是我的excel屏幕打印:

试试这个:

Sub Sheet1()
    Dim rng As Range, cl As Range

    For Each cl In Worksheets("Sheet1").Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        If cl.Offset(0, 1).Interior.Color = 255 Or cl.Offset(0, 2).Interior.Color = 255 Or cl.Offset(0, 3).Interior.Color = 255 Then
            cl.Interior.Color = 255
        End If
    Next cl
End Sub

更改单元格引用,使其与列B的示例类似:

Cells(i, 2)
如果要使用Range,可以使用尝试过的样式,但需要将值连接到A1样式的单元格引用中,如下所示:

Range("B" & i)
我认为问题出在lastR=SheetsSheet1.CellsRows.Count,A.EndxlUp.Row

SheetsSheet1.CellsRows.Count,A将查找最后一个单元格,然后.EndxlUp将再次将所选内容带到第一行

当你说i=lastR到2步骤-1时,你可能想用-1步骤从第一行到第二行。这是不可能的