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

所以我build立了一个条件格式的VBAmacros来突出显示两个单元格:一个是给定的string,另一个是它旁边的。

数据集是:

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)" line改为"=AND($B2=""High"",A1)"那么excel会以红色突出显示它上面的2个单元格,即主机。

任何人都可以帮我突出显示上面的4个单元格和stringsearch词下面的8个单元格(即端口,描述,解决scheme和参考单元格)?

如果您将"=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")

但是这会导致糟糕的performance,因为它是作为一个数组公式工作的。