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

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

我想要突出显示目标范围内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 

以防万一,这有助于:更改:

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

至:

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

你可以尝试的另一件事是replace

 For Each watchCell In WatchRange.Cells 

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

编辑:你还需要两个“Next”语句来匹配它。 所以一定要做到

  Next watchCell Next area