How to delete strikethrough without changing color, bold, italic settings?

167 views Asked by At

I have cells with partial strikethrough. In order to make easier to read, i'd like to delete it.

I've found this VBA, but it doesnt keep the color, or the bold characters.

Any chance you can help me on this?

PS: All the cells with text start with '

Sub DelStrikethroughText()
    Dim xRg As Range, xCell As Range
    Dim xStr As String
    Dim I As Long
    On Error Resume Next
    Set xRg = Application.InputBox("Please select range:", "KuTools For Excel", Selection.Address, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    Application.ScreenUpdating = Fase
        For Each xCell In xRg
            If IsNumeric(xCell.Value) And xCell.Font.Strikethrough Then
                xCell.Value = ""
            ElseIf Not IsNumeric(xCell.Value) Then
                For I = 1 To Len(xCell)
                    With xCell.Characters(I, 1)
                        If Not .Font.Strikethrough Then
                            xStr = xStr & .Text
                        End If
                    End With
                Next
                xCell.Value = xStr
                xStr = ""
            End If
        Next
    Application.ScreenUpdating = True
End Sub
1

There are 1 answers

1
Tim Williams On

You can't replace the value in a cell with partial formatting without losing any per-character formatting.

If you need to remove text then do that using the Characters() methods.

Sub DelStrikethroughText()
    Dim xRg As Range, xCell As Range
    Dim xStr As String
    Dim i As Long, v
    
    On Error Resume Next
    Set xRg = Application.InputBox("Please select range:", "KuTools For Excel", Selection.Address, , , , , 8)
    On Error GoTo 0
    If xRg Is Nothing Then Exit Sub
    
    Application.ScreenUpdating = False
    For Each xCell In xRg
        v = xCell.Value
        If IsNumeric(v) And xCell.Font.Strikethrough Then
            xCell.Value = ""
        ElseIf Not IsNumeric(v) Then
            For i = Len(v) To 1 Step -1
                With xCell.Characters(i, 1)
                    If .Font.Strikethrough Then
                        .Text = "" 'remove the character
                    End If
                End With
            Next
        End If
    Next
    Application.ScreenUpdating = True
End Sub