一个条件格式宏覆盖另一个

One Conditional formating macro overwrite the other

我有两个 VBA 宏,它们在 excel sheet 中执行一些条件格式设置,它们分开工作 - 但是当我 运行 两个宏中一个覆盖另一个在格式规则中,我根本看不出要做什么

 Sub BetingetFormateringAktiviteter()
'
' Makro1 Makro
' Betinget formateringsregler alle Aktiviteter

    Dim rng As Range, offrng As Range
    Dim arrAkt() As Variant
    Dim arrCol() As Variant
    Dim iCount As Integer
    Set rng = Range("J10:J40,Q10:Q39,X10:X40,AE10:AE39,AL10:AL40,AS10:AS40,AZ10:AZ39,BG10:BG40,BN10:BN39,BU10:BU40,CB10:CB40,CI10:CI38,CP10:CP40")
    Set offrng = rng.Offset(0, 1)
    arrAkt = Array("""S""", """A""", """FE""", """SF""", """T""", """TK""", """TH""", """SY""", """V""", """VA""", """L1""", """L2""")

    arrCol = Array(RGB(255, 255, 0), RGB(0, 255, 255), RGB(255, 192, 0), RGB(255, 192, 0), RGB(49, 255, 33), RGB(0, 176, 240), RGB(255, 204, 255), RGB(255, 0, 0), RGB(184, 204, 228), RGB(230, 184, 183), RGB(226, 107, 10), RGB(196, 189, 151))
    iCount = 0
    'Debug.Print offrng.Address: Stop
    
    
    Dim s As Variant
    For Each s In arrAkt
        With rng
            .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="= " & s
            .FormatConditions(1).SetFirstPriority
            With .FormatConditions(iCount + 1).Interior
                .PatternColorIndex = xlAutomatic
                .Color = arrCol(iCount)
                .TintAndShade = 0
            End With
            .FormatConditions(1).StopIfTrue = False
        End With
        With offrng
            .FormatConditions.Add Type:=xlExpression, Formula1:="=" & rng.Cells(1).Address(0, 0) & "= " & s
            .FormatConditions(iCount + 1).Interior.Color = rng.FormatConditions(iCount + 1).Interior.Color
            .FormatConditions(iCount + 1).StopIfTrue = False
        End With
        iCount = iCount + 1
    Next
End Sub

Sub BetingetFormateringLøSøHelligdage()
'
' Makro1 Makro
' Betinget formateringsregler alle Aktiviteter

    Dim rng As Range, offrng As Range
    Set rng = Range("G10:G40,N10:N39,U10:U40,AB10:AB39,AI10:AI40,AP10:AP40,AW10:AW39,BD10:BD40,BK10:BK39,BR10:BR40,BY10:BY40,CF10:CF38,CM10:CM40")
    Set offrngR3 = rng.Offset(0, 3)
    Set offrngR4 = rng.Offset(0, 4)
    Set offrngL1 = rng.Offset(0, -1)
    Set offrngL2 = rng.Offset(0, -2)

    'Debug.Print offrng.Address: Stop
    
    Dim s As Variant

    With rng
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:=" SAND"
        .FormatConditions(1).SetFirstPriority
        With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 12632256
            .TintAndShade = 0
        End With
        .FormatConditions(1).StopIfTrue = False
    End With
    With offrngL1
        .FormatConditions.Add Type:=xlExpression, Formula1:="=" & rng.Cells(1).Address(0, 0) & "= SAND"
        .FormatConditions(1).Interior.Color = rng.FormatConditions(1).Interior.Color
        .FormatConditions(1).StopIfTrue = False
    End With

    With offrngL2
        .FormatConditions.Add Type:=xlExpression, Formula1:="=" & rng.Cells(1).Address(0, 0) & "= SAND"
        .FormatConditions(1).Interior.Color = rng.FormatConditions(1).Interior.Color
        .FormatConditions(1).StopIfTrue = False
    End With
    
    With offrngR3
        .FormatConditions.Add Type:=xlExpression, Formula1:="=" & rng.Cells(1).Address(0, 0) & "= SAND"
        .FormatConditions(1).Interior.Color = rng.FormatConditions(1).Interior.Color
        .FormatConditions(1).StopIfTrue = False
    End With
    
    With offrngR4
        .FormatConditions.Add Type:=xlExpression, Formula1:="=" & rng.Cells(1).Address(0, 0) & "= SAND"
        .FormatConditions(1).Interior.Color = rng.FormatConditions(1).Interior.Color
        .FormatConditions(1).StopIfTrue = False
    End With
    
End Sub

条件格式基于一组相继应用的规则。所以,如果您不希望规则二覆盖规则一,您可以将它们的顺序颠倒过来。

为了验证这是否可行,您可以检查条件格式的规则,并尝试改变后续规则的顺序,如屏幕截图中的蓝色矩形所示(请原谅荷兰人,我没有英语 Excel),您可能会使用“条件格式”、“管理规则”找到它:

供您参考,句子“Regel (toegepast in aangegeven volgorde)”的意思是“规则(按指定顺序应用)”。

另一个选项是复选框“Stoppen indien Waar”,意思是“为真时停止”。选中此项后,一旦找到“TRUE”值,条件格式就会停止,这是防止一个规则覆盖另一个规则的另一种方法。