1

我有一个观察范围,其中某些单元格以黄色突出显示。对于这些单元格中的每一个,都有一个对应的参考值都位于一列中。

我想突出显示目标范围内的所有单元格,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
4

2 回答 2

1

以防万一这有帮助:更改:

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

至:

Set WatchRange = Range("B3:B274,E3:E274,H3:H274,K3:K274")
于 2009-10-14T21:50:49.093 回答
1

您可以尝试的另一件事是更换

For Each watchCell In WatchRange.Cells

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

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

    Next watchCell
Next area
于 2009-10-14T22:00:37.113 回答