首页 文章

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

提问于
浏览
1

我在运行时创建一个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

那么,有多种条件格式(可能重叠范围)的最佳方法是什么,并且它们仍能按预期运行?我已经尝试过调试这么多,我确信有一些我很容易忽视的东西 . 我还尝试了一些不同的方法来指定单独的格式条件(1)和格式条件(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,它也会以某种方式被覆盖 . 我在这里错过了什么吗?

2 回答

  • 1

    抱歉 . 我没有Excel 2007.在Excel 2010中测试过 .

    在条件格式化方面,你必须非常小心宏录制器吐出的内容 . 这是一个特殊情况,它会使代码混乱 .

    你也设置第二个规则为 .SetFirstPriority 这是不正确的,除了让第二个规则运行尽管 rule 1 得到满意:)

    这是一个非常基本的例子 . 让我们说我的范围看起来像这样 D5:G7

    enter image description here

    现在这是我测试的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
    

    这就是我得到的结果 .

    enter image description here

    我相信你很容易将上面的代码移植到vb6 .

    FOLOWUP(来自评论)

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

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

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

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

相关问题