VBA条件格式不适用

时间:2014-11-20 12:19:45

标签: vba conditional-formatting

我有一个例程循环遍历工作簿中所有工作表的所有列,以将条件格式设置规则应用于所有这些列。该规则保存在每个列上,并且我使用的规则有效,但在运行例程后颜色不会改变。然后我可以进入工作表,选择条件格式,点击'编辑规则' > ' OK' > ' OK'然后格式化然后在工作表上更新,而不必更改有关规则的任何内容。为了让规则真正对工作表进行更改,我缺少什么?

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

下一步

1 个答案:

答案 0 :(得分:0)

尝试 ThisWorkbook.RefreshAll Application.Calculate