How do I remove the characters?

前端 未结 4 1638
一向
一向 2021-01-17 18:58

How do I remove special characters and alphabets in a string ?

 qwert1234*90)!  \' this might be my cell value

I have to convert it to

4条回答
  •  别那么骄傲
    2021-01-17 19:50

    You need to use a regular expression.

    See this example:

    Option Explicit
    
    Sub Test()
        Const strTest As String = "qwerty123 456 uiops"
        MsgBox RE6(strTest)
    End Sub
    
    Function RE6(strData As String) As String
        Dim RE As Object, REMatches As Object
    
        Set RE = CreateObject("vbscript.regexp")
        With RE
            .MultiLine = False
            .Global = True
            .IgnoreCase = True
            .Pattern = "([0-9]| )+"   
        End With
    
        Set REMatches = RE.Execute(strData)
        RE6 = REMatches(0)
    
    End Function
    

    Explanation:
    Pattern = "([0-9]| )+" will match any 0 or more group (+) containing a number ([0-9]) or (|) a space ().

    Some more info on the regexp:

    • a thread on ozgrid
    • a very good reference about regexp

提交回复
热议问题