使用参考范围的Excel条件格式

时间:2009-10-14 20:51:40

标签: excel excel-vba formatting conditional conditional-formatting vba

我有一个手表范围,某些单元格突出显示为黄色。对于这些单元中的每一个,存在全部位于一列内的相应参考值。

我想突出显示目标范围red中的所有单元格,其中相应参考单元格中的值与目标单元格中​​的值匹配。

我提出的代码如下,但是有一些编译错误,我无法解决。显然,手表范围不能包含“多个范围”。

Sub Highlight_pairAB()
Dim WatchRange As Range, Target As Range, cell As Range, ref As Range
Set Target = Range("Y3:Y274", "AC3:AC274") 'change column ref as required
Set WatchRange = Range("B3:B274", "E3:E274", "H3:H274", "K3:K274")
Set RefRange = Range("A3:A102")

For Each cell In Target.Cells
    If Application.WorksheetFunction.CountIf(WatchRange, cell.Value) > 0 Then
        cell.Interior.ColorIndex = 3
        For Each watchCell In WatchRange.Cells
            If watchCell.Interior.ColorIndex = 6 And RefRange.Value = Target.Value Then: targetCell.Interior.ColorIndex = 3
        Next watchCell
    Else: cell.Interior.ColorIndex = xlNone
    End If
Next cell
End Sub

2 个答案:

答案 0 :(得分:1)

以防这有助于: 变化:

Set WatchRange = Range("B3:B274", "E3:E274", "H3:H274", "K3:K274") 

要:

Set WatchRange = Range("B3:B274,E3:E274,H3:H274,K3:K274")

答案 1 :(得分:1)

你可以尝试的另一件事是替换

For Each watchCell In WatchRange.Cells

For Each area In WatchRange.Areas
     For Each watchCell In area.Cells

编辑:您还需要两个“下一步”语句才能匹配。所以一定要做到这一点

    Next watchCell
Next area
相关问题