选择两个单元格的值条件并为该行着色

时间:2017-10-13 17:43:16

标签: vba if-statement

在下面的代码中,如果代码找到" 0"在G列的单元格中,该行中的几个单元格是彩色的,我希望这些单元格只有在值为" 0"在同一行的G列和H列中找到,如何做到这一点?

With Worksheets("Sheet3")


    For Each cell In Range("G2:G" & LastRow)
        If cell.Value = "0" Then
            cell.Range("A1:F1").Offset(0, -5).Interior.ColorIndex = 20
        ElseIf cell.Value = "1" Then
            cell.Range("A1:F1").Offset(0, -5).Interior.ColorIndex = 43
        Else
            cell.EntireRow.Interior.ColorIndex = xlNone
        End If
    Next
End With

1 个答案:

答案 0 :(得分:1)

您的代码可以像这样简化为G和H中给定行的A到F的单元格着色:

With Worksheets("Sheet3")
    For Each cell In Range("G2:G" & LastRow)
        currentRow = cell.Row '<-- change
        If cell.Value = 0 And Range("H" & currentRow).Value = 0 Then '<-- change
            Range("A" & currentRow & ":F" & currentRow).Interior.ColorIndex = 20 '<-- coloring from A to F of the same row
        End If
    Next cell '<-- change
End With