围绕给定值的条件格式化单元格

时间:2015-05-21 10:13:41

标签: excel vba excel-vba

所以我设置了一个条件格式化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

这突出显示了具有'高'在其中,以及左侧的单元格,' Severity'。

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

任何人都可以帮助我突出显示上面的4个单元格以及字符串搜索项下面的8个单元格(即端口,描述,解决方案和参考单元格)?

1 个答案:

答案 0 :(得分:1)

如果您“将var clientIp = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty??new RemoteEndpointMessageProperty("",0); 行更改为"=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

但是这会导致性能不佳,因为它是一个数组公式。