【问题标题】:Conditional Formatting Excel document via VB6 (issue with overwriting formats)通过 VB6 条件格式化 Excel 文档(覆盖格式问题)
【发布时间】: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


【解决方案1】:

对不起。我没有 Excel 2007。在 Excel 2010 中对此进行了测试。

当涉及到条件格式时,您必须非常小心宏记录器吐出的内容。这是导致代码混乱的一种特殊情况。

此外,您将第二条规则设置为.SetFirstPriority,这是不正确的,除了让第二条规则运行,尽管rule 1 得到满足:)

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

现在这是我测试的 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 会非常容易。

后续(来自评论)

使用后期绑定...早期绑定是否更适合进行这种类型的条件格式化? – 伯纳德 2 分钟前

如果您使用的是 LateBinding,请在代码顶部声明此代码

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

【讨论】:

  • 我将使用我正在使用的更新代码重新编辑我的原始帖子,但仍然遇到同样的问题。不知何故,第一个条件格式(应用于 D5:工作表结尾)使用绿色文本而不是红色文本。它以某种方式与第二种格式有关(尽管它不在该范围内)。注释掉第二个范围选择/格式允许红色文本正确显示。并且注释掉第一个范围选择/格式允许第二种条件格式正常工作。他们只是不能很好地相处。
  • 在您的帖子中查看我的评论。
【解决方案2】:

经过深思熟虑和重新编写代码,我们得出结论,我正在做的事情(多个条件重叠)是导致混合结果的原因。在最简单的级别上,我能够将 .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 提供的所有帮助。

【讨论】:

    猜你喜欢
    • 2015-04-20
    • 2015-11-05
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    相关资源
    最近更新 更多