Excel VBA根据不同单元格中的值填充单元格颜色

我正在寻找修改我的代码,只填充红色的单元格,如果值= 0和列L中的相邻单元格值为空。

对于值为0的单元格,列L中的相邻单元格值不是空的,我想用模式填充0单元格。

我附上了它应该看起来的截图。

在这里输入图像说明

Sub Fill_Cell() Dim rngCell As Range For Each rngCell In Range("C7:K100") If rngCell.Value = "0" Then rngCell.Cells.Interior.ColorIndex = 3 Else ''If value = 0 & adjacent cell in column L <> '' ''rngCell.Cells.Interior.Pattern = xlGray16 End If Next End Sub 

尝试这个

 Option Explicit Sub Fill_Cell() Dim rngCell As Range For Each rngCell In Range("C7:K100") If rngCell.Value = "0" Then Select Case Cells(rngCell.row, "L") Case Is = "" rngCell.Cells.Interior.ColorIndex = 3 Case Else rngCell.Cells.Interior.Pattern = xlGray16 End Select End If Next rngCell End Sub 

你近了! 我会尽量把你的代码修改一下

 Sub Fill_Cell() Dim rngCell As Range For Each rngCell In Range("C7:K100") If rngCell.Value = "0" Then If Cells(rngCell.Row, 12).Value <> "" Then 'If it is 0 and L not empty rngCell.Cells.Interior.Pattern = xlGray16 else 'If it is 0 and L is empty rngCell.Cells.Interior.ColorIndex = 3 End If End If Next End Sub 

好像有几个人殴打我:

  For Each rngCell In Range("C7:K100") If rngCell.Value = "0" And Cells((rngCell.Row), 12).Value <> "" Then rngCell.Cells.Interior.Pattern = xlGray16 ElseIf rngCell.Value = "0" Then rngCell.Cells.Interior.ColorIndex = 3 End If Next