【问题标题】:How to use VBA for conditional formatting on the same range如何使用 VBA 在同一范围内进行条件格式设置
【发布时间】:2017-06-26 22:03:10
【问题描述】:

我正在尝试使用 VBA 对类似范围进行条件格式设置。我确信我的代码中的错误与优先级有关,但我无法弄清楚它是什么。我正在尝试格式化基本上相同的单元格组。如果 CI 列包含文本“TIES MATERIAL”,那么它应该将列 CU:DD 中的单元格格式化为该特定行的白色。如果该列不包含文本字符串并且值已从其原始值更改,则单元格应更改为红色。

这里是我让它变白的代码:

 Private Sub white_format()
    'This section adds the formatting condition of white cells to the cells that are changed by the PEMCON

    ActiveWorkbook.Sheets("Material").Activate

    Dim lRow As Integer
    Dim lCol As Integer

    lRow = ActiveWorkbook.Sheets("Material").Range("A2").End(xlDown).Row
    lCol = ActiveWorkbook.Sheets("Material").Range("A2").End(xlToRight).Column

    firstCell = ActiveWorkbook.Sheets("Material").Range("CU3").Address(False, False)
    lastCell = ActiveWorkbook.Sheets("Material").Cells(lRow, lCol).Address(False, False)

    Debug.Print "firstCell: " & firstCell
    Debug.Print "lastHeaderCell: " & lastHeaderCell
    Debug.Print "colCount: " & colCount

    'Defines the array of the CU3 to the last used cell and it checks to see if the coorisponding cell in column CI has TIES MATERIAL in it
        ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions.Add Type:=xlExpression, Formula1:="=$CI3=""TIES MATERIAL"""
        ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).SetFirstPriority

        With ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 16777215 'this is the color white
            .TintAndShade = 0
        End With
        ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).StopIfTrue = True


    End Sub

这是我将其设置为红色的代码:

Private Sub Red_Format()

Dim lRow As Integer
Dim lCol As Integer

lRow = ActiveWorkbook.Sheets("Material").Range("A2").End(xlDown).Row
lCol = ActiveWorkbook.Sheets("Material").Range("A2").End(xlToRight).Column

firstCell = ActiveWorkbook.Sheets("Material").Range("CU2").Address(False, False)
lastCell = ActiveWorkbook.Sheets("Material").Cells(lRow, lCol).Address(False, False)
formatRange = ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell)

lastHeaderCell = ActiveWorkbook.Sheets("Material").Cells(2, lCol).Address(False, False)
colCount = ActiveWorkbook.Sheets("Material").Range(firstCell, lastHeaderCell).Columns.Count

'Defines the array of the CU2 to the last used cell and adds the formatting to turn it red if it has been altered.
    ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions.Add Type:=xlExpression, Formula1:="=OFFSET($A$1,ROW()-1,COLUMN()-1)<>OFFSET($A$1,ROW()-1,COLUMN()+" & colCount & ")"
    'ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).SetFirstPriority

    With ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).StopIfTrue = False


End Sub

当我调用white_format 然后在同一个子例程中调用red_format 时,条件格式如下所示。

公式正确显示,但颜色位于它们需要位于的相反部分。我做错了什么?我也知道我的代码并不是它可能/应该是最有效的。不然怎么改?

【问题讨论】:

标签: vba excel conditional-formatting


【解决方案1】:
Formula1:="=$CI3=""TIES MATERIAL"""

“如果 CI 列包含文本“TIES MATERIAL”,那么它应该将单元格格式化为白色”

为此,您的格式条件应为:

Formula1:="=ISNUMBER(SEARCH(""TIES MATERIAL"", $CI" & firstCell.Row & "))"

至于第二个条件,我仍然不明白您想要实现的想法。但是公式可能是正确的,但问题是您错误地引用了它:

With ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).Interior

因为这是 第二个 FormatCondition,您应该将其称为索引(2)。这解释了为什么您实际上用红色覆盖了第一个条件的格式,而第二个条件没有设置格式。

With ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(2).Interior
'                                                                                 ^^^

(这假设您的两个 CF 都适用于同一范围)。如果没有,通常安全的方法是直接在 CF 上获取引用并使用它:

With myRange.formatConditions.Add(xlExpression, formula1)
  .Interior.ColorIndex = ...
  . etc...
End With

【讨论】:

    【解决方案2】:

    公式正确显示,但颜色位于它们需要位于的相反部分。

    这并不完全正确。您创建一个具有白色背景的 CFR 并将其设置为列表顶部。然后创建第二个,但在将其置于列表顶部之前,将列表顶部的 CFR 更改为红色背景。因此,您有一个 CFR,以前是白色背景,现在是红色背景,还有一个没有背景的 CFR。

    我将假设红色 CFR 的公式是正确的。其他人建议进行非易失性更改。

    Option Explicit
    
    Private Sub white_red_CFRs()
        'This section adds the formatting condition of white cells to the cells that are changed by the PEMCON
        Dim lRow As Long, lCol As Long, firstCell As String, lastCell As String
        Dim colCount  As Long, lastHeaderCell  As Long
    
        With ActiveWorkbook.Worksheets("Material")
            lRow = .Range("A2").End(xlDown).Row
            lCol = .Range("A2").End(xlToRight).Column
            firstCell = .Range("CU3").Address(False, False)
            lastCell = .Cells(lRow, lCol).Address(False, False)
    
            With .Range(firstCell, lastCell)
                .FormatConditions.Delete
                With .FormatConditions.Add(Type:=xlExpression, Formula1:="=$CI3=""TIES MATERIAL""")
                    .Interior.Color = 16777215 'this is the color white
                    .SetFirstPriority
                    .StopIfTrue = True
                End With
    
                With .FormatConditions.Add(Type:=xlExpression, Formula1:="=OFFSET($A$1,ROW()-1,COLUMN()-1)<>OFFSET($A$1,ROW()-1,COLUMN()+" & colCount & ")")
                    .Interior.Color = 255 'this is the color red
                    .StopIfTrue = True
                End With
            End With
        End With
    
    End Sub
    

    当您记录 CFR 时,将您的操作转换为代码的 Excel 部分不知道已经存在多少 CFR,因此它会将每个 CFR 设为第一个,以便继续配置并将新 CFR 称为 @987654322 @。在将第二个 CFR 设置为顶部 (1) CFR 之前,您将格式配置设置为 .FormatConditions(1)。我更喜欢使用 With .Add 方法。

    【讨论】:

    • 我们都还没猜到是什么意思,“值已经从原来的值改变了”。我唯一可以怀疑的是colCount左移了一些数据副本...
    【解决方案3】:

    好的。同样,由于您没有提供测试材料,因此有点难以理解您试图实现的目标以及出了什么问题(并且列在右侧很远,我不得不使其更简单......),但这可能只是我。

    要在 CI 列读取 TIES MATERIAL 时将单元格颜色格式化为白色,您的规则公式 "=$CI3=""TIES MATERIAL""" 工作正常,但我只是想知道当它们不是白色时它们会是什么颜色?他们会因为其他规则而变红吗?然后它们的顺序错误,因为冲突的规则将在要应用的列表中占据更高的规则。 “如果为真则停止”适用于 2007 年之前的 Excel 版本。

    并且可以在图像中看到一个错误,它来自您的代码:在“管理规则”对话框图像中,您会看到“白色规则”被标记为“未设置格式”。那是因为您在这两个过程中都引用了 FormatConditions(1)。如果您首先运行“白色规则”和“红色”,则后者已设置好,但同时破坏了第一个(第一)很可能是因为对范围的引用不匹配。

    所以也许您想先运行“白色规则”并在创建“红色”时参考 FormatConditions(2),但正如我所说的很难说。 :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-24
      相关资源
      最近更新 更多