【发布时间】:2016-11-16 06:04:08
【问题描述】:
我正在运行时创建一个 Excel 文档,其中包含一堆我希望有条件地格式化的值。在从头开始进行各种尝试以及使用/修改从 Excel 的宏记录器输出的代码时,我遇到了与格式覆盖相关的一致问题。
我已经发布了下面代码的 sn-p 并且可以说我已经测试以确保我的选择范围是有效的并且适合我想要有条件地格式化的内容。有一些重叠,但奇怪的是第一种条件格式只具有第二种条件格式的一个属性。含义 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,它也会以某种方式被覆盖。我在这里遗漏了什么吗?
【问题讨论】:
-
我试图理解这一点。你可以举个例子来帮助我。假设第一个范围是
A1:G1,第二个范围是D1:J1,所以D1:G1上有一个重叠。那么你期待什么呢? -
我只是得到了很多混合结果,其中不重叠的区域仍然收到与第二种条件格式相关的奇怪条件格式。很抱歉,这有点难以理解,但我会尝试在我的原始帖子中模拟一个示例。
-
是的,如果处理不当,条件格式可能是 PIA... 将等待您的示例。
-
我正在做更多的测试,看看我是否能弄清楚为什么只有一种条件格式的一部分格式被应用到其他地方。当您处理像字体颜色这样简单的事情时(而且您缺乏红色/绿色),这有点令人沮丧。无论如何,感谢您愿意帮助@SiddharthRout。我期待您的帮助。
-
只是为了确定..您使用的是哪个 excel 版本?
标签: vba excel vb6 conditional-formatting