应用条件格式后如何删除?

How do I removed Conditional Formatting after its been applied?

我有一个工作表更改宏,如果最后一个单元格包含“已取消”一词,它会突出显示一行中的前 8 个单元格。这很好用。然而,取消这个词在下拉菜单中,如果你不小心 select 它宏就会启动。如果你在同一个单元格中更改为另一个词,我希望它删除条件并恢复正常。有人可以帮我解决这个问题吗?我确定我缺少的是简单的东西。

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo ErrHandler
   
    If UsedRange.Rows.Count > 0 Then
        If Trim(Cells(Target.Row, Target.Column)) <> "" And _
            UCase(Cells(Target.Row, Target.Column)) = "CANCELLED" Then
            
            Range(Cells(Target.Row, 1), Cells(Target.Row, 8)).Interior.Color = vbRed
            Range(Cells(Target.Row, 1), Cells(Target.Row, 8)).Font.Color = vbWhite
       
        ElseIf Trim(Cells(Target.Row, Target.Column)) <> "" And _
            UCase(Cells(Target.Row, Target.Column)) <> "CANCELLED" Then
         
                
            Cells.FormatConditions.Delete
            
        
        
        End If
    End If
ErrHandler:
    '
End Sub

您不会“应用”和“删除”。你在这两种情况下都“应用”,只是你应用了不同的颜色。

Private Sub Worksheet_Change(ByVal Target As Range)

    Const TriggerClm    As Long = 8         ' change to suit
    Dim TriggerRng      As Range
    Dim TargetRng       As Range
    Dim IntCol          As Long
    
    ' Here the first row is 2, presuming row 1 contains captions
    Set TriggerRng = Range(Cells(2, TriggerClm), Cells(Rows.Count, TriggerClm).End(xlUp))
    
    If Not Application.Intersect(Target, TriggerRng) Is Nothing Then
        With Target
            Set TargetRng = Range(Cells(.Row, TriggerClm - 7), Cells(.Row, TriggerClm))
            If StrComp(CStr(.Value), "cancelled", vbTextCompare) Then
                TargetRng.Interior.Pattern = xlNone
                TargetRng.Font.Color = vbBlack
            Else
                TargetRng.Interior.Color = vbRed
                TargetRng.Font.Color = vbWhite
            End If
        End With
    End If
End Sub

请注意,我推断只有第 8 列中的单元格发生更改时才能触发更改,因为只有该单元格是“已取消”或未“取消”。我的代码逻辑在这方面与你的有偏差。