我正在运行时创建一个Excel文档,该文档具有一堆我希望有条件格式化的值。从头开始进行各种尝试,以及使用/修改从Excel宏记录器输出的代码,我遇到了与格式化覆盖有关的一致问题。
我在下面发布了一段代码,可以说我已经过测试,以确保我的选择范围有效且适合于我想要有条件格式化的内容。有一些重叠,但是奇怪的是,第一种条件格式仅具有第二种条件格式的一个属性。含义D5:工作表的结尾最终具有绿色字体,而不是应具有的红色字体。注释代码的每个部分都可以使它们独立工作,但是我猜这是进一步以某种方式指定条件格式的问题吗?我尝试了几种不同的情况,下面是经过修改的代码:
编辑(更新代码):
'First conditional format, check sheet for values > 50 and make text red.
With xl.range("D5:" & theLastColumn & lastRow)
.FormatConditions.add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=50"
With .FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
'Second conditional format, check specific row (row 5 in the example)
'for values > 40, and fill interior with green in addition to dark green text.
With xl.range("D" & Infectivity & ":" & theLastColumn & Infectivity)
.FormatConditions.add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=40"
With .FormatConditions(2).Font
.Color = -16752384
.TintAndShade = 0
End With
With .FormatConditions(2).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
End With
那么拥有多种条件格式(可能会重叠范围)并且仍然使它们全部按预期发挥作用的最佳方法是什么?我已经尝试过调试太多了,我敢肯定有一些容易被我忽略的事情。我还尝试了几种不同的方法来指定单独的formatconditions(1)和formatconditions(2),但仍然遇到奇怪的问题。
编辑:
我仍然遇到相同问题的VBA代码。
Sub conditionalFormat()
With Range("D5:BA9")
.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=50"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
With Range("D9:BA9")
.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=40"
With .FormatConditions(2).Font
.Color = -16752384
.TintAndShade = 0
End With
With .FormatConditions(2).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
.FormatConditions(2).StopIfTrue = False
End With
End Sub
即使将SetFirstPriority设置为适当的(红色文本)条件格式,它仍会以某种方式被覆盖。我在这里想念什么吗?
经过深思熟虑并重新编写了代码,我们得出的结论是,我所做的事情(多个条件重叠)是导致结果混合的原因。在最简单的级别,我能够将.FormatConditions.Delete添加到其他条件格式中,以确保仅应用了一种格式。
更正后的最终代码如下所示:
Dim Infectivity As Long
Infectivity = Application.WorksheetFunction.match("Infectivity", range("A1:" & "A" & lastRow), 0)
With xl.range("D5:" & theLastColumn & lastRow)
.FormatConditions.add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=50"
.FormatConditions(.FormatConditions.count).SetFirstPriority
With .FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
If Infectivity > 0 Then
With xl.range("D" & Infectivity & ":" & theLastColumn & Infectivity)
.FormatConditions.Delete
.FormatConditions.add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=40"
With .FormatConditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
End If
我的失败与宏记录器有关,这使我错失了格式化这些单元格的理想方法。最好在继续前进之前先进行简化。
非常感谢Siddharth Rout的所有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句