Excel VBA: Delete strikethrough characters in a ce

2019-08-03 21:10发布

I have around 1000 rows of data. Some cells consist of both Strikethrough and normal characters. I would like to only delete those character that are Strikethrough and keeping the other non-Strikethrough character in those cells. The rest of the cell should also be left untouched.

Example in a cell:

"334(strikethrough) 124"

The code should change the cell value to:

"124"

without deleting the whole cell.

2条回答
贼婆χ
2楼-- · 2019-08-03 21:36

try this

Sub Macro1()
Dim oCell As Range, i%, z%
For Each oCell In Range("A1:A6")
    z = Len(oCell.Value)
    i = 1
    While z <> 0
        If oCell.Characters(Start:=i, Length:=1).Font.Strikethrough = True Then
            oCell.Characters(Start:=i, Length:=1).Caption = ""
            i = i - 1
        End If
        i = i + 1
        z = z - 1
    Wend
Next
End Sub
查看更多
地球回转人心会变
3楼-- · 2019-08-03 21:44

When deleting from Characters it's easier to loop right to left:

Sub Demo()
    Dim rng As Range, cl As Range
    Dim i As Long

    Set rng = [A1:A3] ' Get range of interest

    For Each cl In rng.Cells
    For i = Len(cl.Value) To 1 Step -1
        If cl.Characters(i, 1).Font.Strikethrough Then
            cl.Characters(i, 1).Delete
        End If
    Next i, cl

End Sub
查看更多
登录 后发表回答