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

问题描述 投票:0回答:2

继作品如预期,但一旦我关闭并重新打开Excel文件失败&只执行一次,如果我在同一列中改变一些东西,添加/删除选项卡或文件加载。击中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年。

谢谢。

excel vba conditional-formatting
2个回答
0
投票

我有Office 2003专业,但这应该仍然适用,因为它可能是事件和地点驱动。

我猜,你是不是在ThisWorkbook对象的Workbook_Open事件运行代码,并在表而不是地方。

因此,你应该把你的代码放到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



0
投票

找到了解决。

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
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.