【问题标题】:VBA conditional formatting not applyingVBA条件格式不适用
【发布时间】:2014-11-20 12:19:45
【问题描述】:

我有一个例程循环遍历工作簿中所有工作表上的所有列,以将条件格式规则应用于所有列。该规则很好地保存在每一列上,并且我使用的规则有效,但是在运行例程后颜色不会改变。然后我可以进入工作表,选择条件格式,单击“编辑规则”>“确定”>“确定”,然后格式会在工作表上更新,而无需更改任何有关规则的内容。为了让规则实际更改工作表,我缺少什么?

For Each ws In ThisWorkbook.Worksheets

If ws.Name Like "*Management*" Then Exit Sub

lastRow = ws.UsedRange.Row + ws.UsedRange.Rows.Count - 1
lastCol = ws.UsedRange.Column + ws.UsedRange.Columns.Count - 1

ws.Cells.FormatConditions.Delete

For col = 1 To lastCol

    Set rng = ws.Range(ws.Cells(2, col), ws.Cells(lastRow, col))
    formulaStr = "=BITWISE_AND(2^INDEX(" & wsSchema.Name & "!$" & schemaLastCol & "$2" & _
        ":$" & schemaLastCol & "$" & schemaLastRow & ", MATCH(1, (" & wsSchema.Name & "!$A$2:$A$" & _
        schemaLastRow & "=""" & ws.Name & """)*(" & wsSchema.Name & "!$B$2:$B$" & schemaLastRow & "=$" & _
        XLCol(col) & "$1),0)), INDEX($" & XLCol(lastCol) & "$1:$" & XLCol(lastCol) & "$" & lastRow & ",ROW()))"

    With rng
        .FormatConditions.Add xlExpression, , formulaStr & " = 0"
        With .FormatConditions(1)
            .Interior.PatternColorIndex = xlAutomatic
            .Interior.Color = RGB(255, 0, 0)
            .Interior.TintAndShade = 0
            .StopIfTrue = False
            .SetFirstPriority
        End With
        .FormatConditions.Add xlExpression, , formulaStr & " > 0"
        With .FormatConditions(2)
            .Interior.PatternColorIndex = xlAutomatic
            .Interior.Color = RGB(0, 255, 0)
            .Interior.TintAndShade = 0
            .StopIfTrue = False
            .SetFirstPriority
        End With
    End With

Next

下一个

【问题讨论】:

  • 尝试录制一个宏,看看在你做编辑规则的时候调用了哪些方法。
  • 我做了,调用的方法都是两个inner with块中包含的,但没有乐趣

标签: vba conditional-formatting


【解决方案1】:

尝试 ThisWorkbook.RefreshAll Application.Calculate

【讨论】:

  • 是的,都试过了,都没有成功。有趣的是,我发现当打开工作簿时,格式已经全部到位。不幸的是,这无助于解决问题,因为工作簿关闭后需要从工作簿中清除所有数据(数据是由另一个例程从 SQL Server 数据库中提取的)
猜你喜欢
  • 2015-02-28
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 1970-01-01
相关资源
最近更新 更多