VBA Tip: Trim

The Trim function allows you to remove unnecessary spaces at the beginning and end of a string.

Use:

Trim(string)


Example

Removal of unnecessary spaces at the beginning and end of the text in cell A3:

function-trim-excel-vba - trim
Sub example()

    my_variable = Trim(Range("A3"))
   
    MsgBox my_variable 'Returns "Excel-Pratique.com"

End Sub

Example 2

Removal of unnecessary spaces at the beginning and end of the text in "my_variable":

Sub example()

    my_variable = "     Excel-Pratique.com          "

    MsgBox Trim(my_variable) 'Returns "Excel-Pratique.com"

End Sub