基于关键字的VBA标记单元

时间:2018-09-05 14:25:26

标签: excel-vba colors cell

我已经根据单元格文本定义了一个用颜色填充的范围。在第一个示例中,它搜索单词“ Auto”并将单元格标记为红色。我遇到的麻烦是扩展“ if”命令,例如搜索条件进入列E(但与单词“ Auto”保持在同一行),在单词“ Mortgage”之后进行标记,并将其标记为红色。然后它将列进一步向上移动,并将红色值标记为4yr,6yr,7yr以下。下一步将与单词“ preferred”相同。为了简单起见,我附带了一张图片。这似乎有点棘手,并且将不胜感激。

enter image description here

 Sub Schaltfläche2_Klicken()
 Dim cell As Range

 For Each cell In ws.Range("A1:A100")
     If cell.Value = "Auto" Then
     cell.Interior.Color = XlRgbColor.rgbRed

     ElseIf cell.Value = "Mutti" Then
     cell.Interior.Color = XlRgbColor.rgbRed
    End If
Next
End Sub

1 个答案:

答案 0 :(得分:0)

以下内容如何:

Sub Schaltfläche2_Klicken()
 Dim cell As Range

 For Each cell In ws.Range("A1:A100")
    If cell.Value = "Auto" Then
        Range("A" & cell.Row, "E" & cell.Row, "G" & cell.Row, "I" & cell.Row, "K" & cell.Row).Interior.Color = vbRed
    ElseIf cell.Value = "Mutti" Then
        Range("A" & cell.Row, "E" & cell.Row, "G" & cell.Row, "I" & cell.Row, "K" & cell.Row).Interior.Color = vbRed
    End If
Next
End Sub