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

发布于 2024-08-08 09:16:29 字数 875 浏览 5 评论 0原文

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

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

我想出的代码如下,但存在某种我无法修复的编译错误。显然手表范围不能由“多个范围”组成。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑咖 2024-08-15 09:16:29

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

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

至:

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

Just in case this helps:
Change:

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

To:

Set WatchRange = Range("B3:B274,E3:E274,H3:H274,K3:K274")
寂寞笑我太脆弱 2024-08-15 09:16:29

您可以尝试的另一件事是

For Each watchCell In WatchRange.Cells

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

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

    Next watchCell
Next area

Another thing you could try would be to replace

For Each watchCell In WatchRange.Cells

with

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

edit: you also need two "Next" statements for it to match up. So be sure to make it

    Next watchCell
Next area
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文