在 Excel VBA 中查看突出显示的选定行文本时,如何禁用和启用条件格式 ("Strikethrough & Red Text")?

How can you disable and enable conditional formatting ("Strikethrough & Red Text") when viewing highlighted selected row text in Excel VBA?

我想在条件显示行文本颜色为红色和删除线的情况下禁用条件格式,然后在移动到另一行后将该行恢复为条件格式。这是否可能而不必删除格式并创建新格式?

Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

        Application.EnableEvents = False
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual

        Static xRow
        Static xColumn

        If xColumn <> "" Then
            With Columns(xColumn).Interior
                .Color = RGB(38, 38, 38) 'dark grey
                .Pattern = xlSolid
            End With
            
            Range("$A:$A2000").Font.Color = vbYellow
            Range("$C:$C2000").Font.Color = vbYellow
            Range("$B:$B2000").Font.Color = vbWhite
            Range("$D:$L2000").Font.Color = vbWhite
            Range("$A:$L").Interior.Color = RGB(38, 38, 38) 'dark grey
            Range("$A:$L").Interior.Color = vbBlack
            
            
            
            With Rows(xRow).Interior
                .Color = RGB(38, 38, 38) 'dark grey
                .Pattern = xlSolid
            End With
            
            Range("$A:$A2000").Font.Color = vbYellow
            Range("$C:$C2000").Font.Color = vbYellow
            Range("$B:$B2000").Font.Color = vbWhite
            Range("$D:$L2000").Font.Color = vbWhite
            Range("$A:$L2000").Interior.Color = RGB(38, 38, 38) 'dark grey
        End If

        pRow = Selection.Row
        pColumn = Selection.Column
        xRow = pRow
        xColumn = pColumn
        
        With Columns(pColumn).Interior
            .Color = RGB(89, 89, 89) 'light grey
            .Pattern = xlSolid
        End With
        
        With Columns(pColumn).Font
             .Color = vbBlack
        End With
        
        With Columns(pColumn).Font
             .Bold = True
        End With
        
        With Columns(pColumn).Font
             .Italic = True

             '### I have tried this and does not work
              '.Strikethrough = False
        End With
        
        With Rows(pRow).Interior
            .Color = RGB(89, 89, 89) 'grey
            .Pattern = xlSolid
        End With
        
        With Rows(pRow).Font
            '.Color = RGB(255, 255, 255) 'white
            '.Color = RGB(38, 38, 38) 'dark grey
            '.Color = RGB(0, 255, 0) 'green
            .Color = vbGreen
            '.Strikethrough = False
        End With
        
        With Rows(pRow).Font
            .Bold = True
        End With
        
        With Rows(pRow).Font
            .Italic = True
        End With
        
        '## This works, but I really do not want to use it if I have to.
        ''Delete Previous Conditional Formats
        'Rows(pRow).FormatConditions.Delete
        
        Selection.Cells.Font.Color = RGB(255, 0, 102) 'pink
        Selection.Cells.Interior.Color = RGB(38, 38, 38) 'dark grey
        

        '## Tried this and does not work as well.
        'Selection.Rows.Font.Strikethrough = False

        Range("$A:$L").Font.Color = RGB(0, 176, 245) 'light blue
        Range("$A:$L").Font.Color = vbYellow
        
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
        Application.EnableEvents = True
End Sub

我编辑了我之前的 (hack-based) 回复,事实证明 Modify 方法使用起来更简单: 下面示例代码的上下文是条件格式已在 UI 中应用到 B10:F15 范围,如果任何行的第一个单元格具有值 Obsolete[=15 则激活=],如果活动单元格在此范围内,则该行的条件格式将为 de-activated:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim cfRange As Range, cfCondition As String
    Set cfRange = Range("B10:F15")
    cfCondition = "=$B10=""Obsolete"""
    If Not Application.Intersect(Target, cfRange) Is Nothing Then
        Dim newCondition As String
        newCondition = Strings.Replace(cfCondition, "=$", "=AND($")
        newCondition = newCondition & ", ROW()<>" & Target.Row & ")"
        cfRange.FormatConditions(1).Modify xlExpression, , newCondition
    Else
        cfRange.FormatConditions(1).Modify xlExpression, , cfCondition
    End If
End Sub