【问题标题】:Conditional Formatting cells surrounding a given value围绕给定值的条件格式单元格
【发布时间】:2015-05-21 10:13:40
【问题描述】:

所以我设置了一个条件格式 VBA 宏来突出显示两个单元格:一个具有给定字符串的单元格,另一个是它旁边的单元格。

数据集是:

A1          B1
------------------------
PluginID    NUM  
Host        ADDRESS  
Severity    High  
Port        PORT  
Description DESCRIPTION  
Solution    SOLUTION  
References  CVE  

VBA 代码是:

    Sub High2()
'
' High2 Macro
'

'
    Columns("A:B").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND($B1=""High"",A1)"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

这会突出显示其中包含“高”的单元格,以及左侧的单元格“严重性”。

如果我将"=AND($B1=""High"",A1)" 行更改为"=AND($B2=""High"",A1)",则 excel 会以红色突出显示其上方的 2 个单元格,即 Host。

谁能帮我突出显示字符串搜索词上方的 4 个单元格和下方的 8 个单元格(即端口、描述、解决方案和参考单元格)?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    如果您“将"=AND($B1=""High"",A1)" 行更改为"=AND($B2=""High"",A1)"”,您实际所做的只是添加一条新规则。所以这确实是最好的方法。根据需要添加尽可能多的规则。

    Sub High2()
     With Columns("A:B").Cells
      .Range("A1").Activate
      .FormatConditions.Delete
      For i = 1 To 3 ' 3 above
       .FormatConditions.Add Type:=xlExpression, Formula1:="=($B" & i & "=""High"")"
       .FormatConditions(.FormatConditions.Count).SetFirstPriority
       With .FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
       End With
       .FormatConditions(1).StopIfTrue = False
      Next
      For i = 0 To 3 ' 4 below
       .FormatConditions.Add Type:=xlExpression, Formula1:="=($B" & .Rows.Count - i & "=""High"")"
       .FormatConditions(.FormatConditions.Count).SetFirstPriority
       With .FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
       End With
       .FormatConditions(1).StopIfTrue = False
      Next
     End With
    End Sub
    

    这也可以通过一条规则来实现:

    =OR($B1048573:$B1048576="High", $B1:$B3="High")

    但这会导致性能不佳,因为它是作为数组公式工作的。

    【讨论】:

    • 太棒了,谢谢阿克塞尔。我会解决这个问题,看看我是否能掌握它。
    猜你喜欢
    • 2011-08-08
    • 2023-03-24
    • 2018-06-04
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    相关资源
    最近更新 更多