Removing All Spaces in String

后端 未结 7 1368
滥情空心
滥情空心 2021-01-11 13:33

I created a macro for removing all whitespace in a string, specifically an email address. However it only removes about 95% of the whitespace, and leaves a few.

My c

7条回答
  •  不知归路
    2021-01-11 14:01

    Just use a regular expression:

    'Add a reference to Microsoft VBScript Regular Expressions 5.5
    Public Function RemoveWhiteSpace(target As String) As String
        With New RegExp
            .Pattern = "\s"
            .MultiLine = True
            .Global = True
            RemoveWhiteSpace = .Replace(target, vbNullString)
        End With
    End Function
    

    Call it like this:

    Sub NoSpaces()
        Dim w As Range
    
        For Each w In Selection.Cells
            w.Value = RemoveWhiteSpace(w.Value)
        Next
    End Sub
    

提交回复
热议问题