Excel VBA: How to remove substrings from a cell?

前端 未结 4 892
离开以前
离开以前 2021-01-06 08:01

I have a cell value like this:

This is a <\"string\">string, It should be <\"changed\">changed to <\"a\"> a number.

There

4条回答
  •  自闭症患者
    2021-01-06 08:51

    Select the cells containing your text and run this short macro:

    Sub Kleanup()
        Dim d As Range, s As String, rng As Range
        Dim gather As Boolean, L As Long, DQ As String
        Dim i As Long, s2 As String, CH As String
    
        Set rng = Selection
        DQ = Chr(34)
    
        For Each r In rng
            s = Replace(r.Text, "<" & DQ, Chr(1))
            s = Replace(s, DQ & ">", Chr(2))
            gather = True
            L = Len(s)
            s2 = ""
            For i = 1 To L
                CH = Mid(s, i, 1)
                If CH = Chr(1) Then gather = False
                If CH = Chr(2) Then gather = True
                If gather And CH <> Chr(2) Then s2 = s2 & CH
            Next i
            r.Value = s2
        Next r
    End Sub
    

提交回复
热议问题