文件重新打开后条件格式失败

conditional formatting fails after file reopen

以下按预期工作,但一旦我关闭并重新打开 excel 文件,它就会失败并且仅在我更改同一列、add/delete 选项卡或加载文件时执行一次。按 F9 什么都不做。如果我手动进入现有的条件格式并重新应用它再次正常工作,尽管没有任何改变,但关闭并重新打开文件和同样的问题。

    rngNew = "D1" & ":" & Cells(lRowEnd, 4).Address
    Set rngShopTime = Range(rngNew)

    Dim txtShopFree As String
    txtShopFree = "=""ShopFree"""
    Dim rngShop As String
    rngShop = "C1" & ":" & Cells(lRowEnd, 3).Address(False, True)
    With rngShopTime
        .FormatConditions.Add Type:=xlExpression, Formula1:="=" & rngShop & txtShopFree
        .FormatConditions(1).StopIfTrue = False
        With .FormatConditions(.FormatConditions.Count)
            .SetFirstPriority
            With .Interior
                .PatternColorIndex = xlAutomatic
                .Color = RGB(128, 128, 128)
                .TintAndShade = 0
            End With
        End With
    End With

我已经隔离了这段代码,它仍然会发生。 我将如何对此进行故障排除或返工,因为我根本没有收到任何错误。

Excel 版本 2013 和 2010。

谢谢。

我有 office 2003 Professional,但这应该仍然适用,因为它可能是事件和位置驱动的。

我猜你不是 运行 ThisWorkbook 对象的 Workbook_Open 事件的代码,而是 sheet.

中的某个地方

因此,您应该将代码放入 Workbook_Open 事件中并尝试一下。

    'This is code on the ThisWorkbook Object
    '-------------------------------------------
    Option Explicit

    Private Sub Workbook_Open()

        Sheet1.Columns("D:D").Select
        Selection.FormatConditions.Delete
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
            Formula1:="0"
        Selection.FormatConditions(1).Interior.ColorIndex = 44
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
            Formula1:="1"
        Selection.FormatConditions(2).Interior.ColorIndex = 35
    End Sub


已找到修复程序。

已更改

rngShop = "C1" & ":" & Cells(lRowEnd, 3).Address(False, True)

rngShop = "C1" & txtShopFree 

    .FormatConditions.Add Type:=xlExpression, Formula1:="=" & rngShop & txtShopFree

    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:="=" & rngShop