【问题标题】:Excel Conditional Formatting Using A Reference Range使用参考范围的 Excel 条件格式
【发布时间】:2010-12-06 20:05:41
【问题描述】:

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

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

【问题讨论】:

    标签: excel formatting conditional conditional-formatting vba


    【解决方案1】:

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

    For Each watchCell In WatchRange.Cells
    

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

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

        Next watchCell
    Next area
    

    【讨论】:

    • 这看起来很有希望,但现在我在这里收到“Else without If”的错误:“Else: cell.Interior.ColorIndex = xlNone”
    【解决方案2】:

    以防万一这有帮助: 变化:

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

    收件人:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      相关资源
      最近更新 更多