VBA 条件搜索和标记

VBA conditional search and marking

目的是在 A 列中搜索单词 "XXX"。当在该列中找到该词时,下一个单元格应与词 "C" 匹配。如果不匹配,请在下一行继续搜索 "XXX",等等。我的问题代码是它标记所有 "C"s。换句话说,没有条件搜索 where XXX=C.

For Each cell In ws.Range("A1:A20").SpecialCells(xlCellTypeConstants)
    Select Case cell.Value2
        Case "XXX"
            col = RGB(202, 225, 255)
        Case Else
            col = 0
    End Select


    If col > 0 Then
        wb.Activate
        cell.Interior.Color = col
        For Each cell2 In cell.Offset(, 2).Resize(1).SpecialCells  (xlCellTypeConstants)
        res = Switch(cell2.Value = "C", vbGreen)
        If Not IsNull(res) Then Intersect(Range("B:B, J:J, L:L, N:N, Q:Q"), Rows(cell2.Row)).Interior.Color = CLng(res) '
        Next
    End If
Next

已编辑:见下文

有点不确定你在问什么,但我认为解决方案如下。 我使用了更简单的代码,但应该足够了。

For xJ = 1 to 200
    If Range("A" & xJ).Value = "XXX" then
        Range("A" & xJ).Interior.Color = RGB(202, 225, 255)
        If Range("B" & xJ).Value = "C" Then
            Range("B" & xJ).Interior.Color = RGB(57, 225, 20)
        End If
    End if
Next xJ