2017-10-13 28 views
0

在下面的代码中,如果代码在列G的单元格中找到“0”,那么该行中的几个单元格将被着色,我希望这些单元格仅在在同一行的G列和H列中发现值“0”,这怎么办?为两个单元格的值选择条件并为行着色

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 
+0

条件格式:'= AND($ G1 = 0 ,$ H1 = 0)' –

回答

1

您的代码可以简化像这样从颜色细胞到F在G和H具有0的给定行的:

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 
+0

测试完你的代码之后我刚注意到代码把空单元看作是它的值为“0”,所以在它旁边的空单元格中也突出显示了单元与值“0”的组合.... – FotoDJ

+0

是的,因为如果单元格是空的,它被视为“零”。您可以添加进一步的测试和cell.Value <>“”以避免这种情况。 –

+0

明白了,谢谢。 – FotoDJ

相关问题