使用参考范围的 Excel 条件格式
我有一个手表范围,其中某些单元格突出显示为黄色。对于每个单元格,都有一个相应的参考值,全部位于一列中。
我想以红色突出显示目标范围内的所有单元格,其中相应参考单元格中的值与目标单元格中的值相匹配。
我想出的代码如下,但存在某种我无法修复的编译错误。显然手表范围不能由“多个范围”组成。
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
I have a watch range with certain cells highlighted yellow. For each of these cells, there is a corresponding reference value all located within one column.
I want to highlight all cells in my target range red
for which the value in the corresponding reference cell matches the value in the target cells.
The code I've come up with is as follows, but there is some sort of a compile error I am unable to fix. Apparently the watch range can't consist of "multiple ranges".
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以防万一这有帮助:
更改:
至:
Just in case this helps:
Change:
To:
您可以尝试的另一件事是
用
编辑替换:您还需要两个“Next”语句才能匹配。所以一定要做到
Another thing you could try would be to replace
with
edit: you also need two "Next" statements for it to match up. So be sure to make it