How to remove spaces in between text?

后端 未结 10 794
日久生厌
日久生厌 2021-01-13 00:00

Why trim is not working in VBA?

for i = 3 to 2000
activesheet.cells(i,\"C\").value = trim(Activesheet.cells(i,\"C\").value)
next i

It is un

10条回答
  •  灰色年华
    2021-01-13 00:44

    Are all your other functions leaving whitespace behind?

    Get CleanUltra!

    CleanUltra removes all whitespace and non-printable characters including whitespace left behind by other functions!

    I hope you find this useful. Any improvements are welcome!

    Function CleanUltra( _
           ByVal stringToClean As String, _
           Optional ByVal removeSpacesBetweenWords As Boolean = False) _
            As String
    ' Removes non-printable characters and whitespace from a string
    
    
    ' Remove the 1 character vbNullChar. This must be done first
    '  if the string contains vbNullChar
        stringToClean = Replace(stringToClean, vbNullChar, vbNullString)
    
        ' Remove non-printable characters.
        stringToClean = Application.Clean(stringToClean)
    
        ' Remove all spaces except single spaces between words
        stringToClean = Application.Trim(stringToClean)
    
        If removeSpacesBetweenWords = True Then _
           stringToClean = Replace(stringToClean, " ", vbNullString)
    
        CleanUltra = stringToClean
    End Function
    

    Here's an example of it's usage:

    Sub Example()
        Dim myVar As String
        myVar = " abc d e  "
    
        MsgBox CleanUltra(myVar)
    End Sub
    

    Here's a test I ran to verify that the function actually removed all whitespace. vbNullChar was particularly devious. I had to set the function to remove it first, before the CLEAN and TRIM functions were used to stop them from removing all characters after the vbNullChar.

    Sub Example()
        Dim whitespaceSample As String
        Dim myVar As String
    
    ' Examples of various types of whitespace
    '  (vbNullChar is particularly devious!)
        whitespaceSample = vbNewLine & _
                           vbCrLf & _
                           vbVerticalTab & _
                           vbFormFeed & _
                           vbCr & _
                           vbLf & _
                           vbNullChar
    
        myVar = "     1234" & _
                whitespaceSample & _
                "     56      " & _
                "789     "
    
        Debug.Print "ORIGINAL"
        Debug.Print myVar
        Debug.Print "Character Count: " & Len(myVar)
    
    
        Debug.Print
        Debug.Print "CLEANED, Option FALSE"
    
    
        Debug.Print CleanUltra(myVar)
        Debug.Print CleanUltra(myVar, False)
        '   Both of these perform the same action.  If the optional parameter to
        '   remove spaces between words is left blank it defaults to FALSE.
        '   Whitespace is removed but spaces between words are preserved.
        Debug.Print "Character Count: " & Len(CleanUltra(myVar))
    
    
        Debug.Print
        Debug.Print "CLEANED, Option TRUE"
    
        Debug.Print CleanUltra(myVar, True)
        '   Optional parameter to remove spaces between words is set to TRUE.
        '   Whitespace and all spaces between words are removed.
        Debug.Print "Character Count: " & Len(CleanUltra(myVar, True))
    
    End Sub
    

提交回复
热议问题