通过VB6条件格式化Excel文档(与覆盖格式相关的问题)

我在运行时创build了一个Excel文档,其中包含了一些我想要有条件格式化的值。 在从零开始进行各种尝试以及使用/修改从Excelmacros录制器输出的代码时,我遇到了与格式化覆盖有关的一致问题。

我已经发布了下面的代码片段,可以说我已经过testing,以确保我的select范围是有效的,适合我想要的条件格式。 有一些重叠,但奇怪的是,第一个条件格式只有第二个条件格式的一个属性。 含义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 

那么有多种条件格式(可能会重叠范围)并且仍然具有所有function的最佳方式是什么? 我已经尝试过debugging这么多,我确定有一些容易忽略的东西。 我也尝试了几种不同的方法来指定单独的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,它也会以某种方式被覆盖。 我在这里错过了什么?

抱歉。 我没有Excel 2007.在Excel 2010中对此进行了testing。

当谈到条件格式时,你必须非常小心macroslogging器吐出的内容。 这是一个混乱的代码的特殊情况。

另外你设置的是第二条规则,除了让第二条规则运行而不是rule 1得到满足之外,不正确的.SetFirstPriority 🙂

这是一个非常基本的例子。 假设我的范围看起来像这个D5:G7

在这里输入图像说明

现在这是我testing的VBA代码

 Sub Sample() Dim ws As Worksheet Dim rng As Range Set ws = ThisWorkbook.Sheets("Sheet1") Set rng = ws.Range("D5:G7") With rng .FormatConditions.Add Type:=xlCellValue, _ Operator:=xlGreater, Formula1:="=50" .FormatConditions(.FormatConditions.Count).SetFirstPriority With .FormatConditions(1).Font .Color = -16776961 .TintAndShade = 0 End With .FormatConditions(1).StopIfTrue = True .FormatConditions.Add Type:=xlCellValue, _ Operator:=xlGreater, Formula1:="=40" With .FormatConditions(2).Font .Color = -11489280 .TintAndShade = 0 End With With .FormatConditions(2).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent3 .TintAndShade = 0.599963377788629 End With End With End Sub 

这是我得到的结果。

在这里输入图像说明

我相信,将上面的代码移植到vb6将会非常容易。

FOLOWUP(来自评论)

使用迟绑定…将earlybinding更适合做这种types的条件格式? – 伯纳德2分钟前

如果您使用的是LateBinding,那么在代码顶部声明这个代码

 Const xlCellValue as Long = 1 Const xlGreater as Long = 5 Const xlAutomatic as Long = -4105 Const xlThemeColorAccent3 as Long = 7 

经过多次思考和修改代码后,我们得出结论:我正在做的事情(多重条件重叠)是混合结果的原因。 在最简单的层面上,我能够将.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 

我的失败与macros观logging器有关,给我一个格式化这些单元格的理想方法。 前进之前总是最好简化一下。

非常感谢Siddharth Rout的所有帮助。