The VBA RTrim function removes the trailing spaces from a supplied text string.
The syntax of the function is:
Where the String argument is the text string that you want to remove the trailing spaces from.
The following example shows how the VBA RTrim function can be used to remove one or more trailing spaces from two different text strings.
' Remove the trailing spaces from two text strings.
Dim name As String
Dim address As String
name = "John Smith " ' has a trailing space
address = "21 Smithson Street " ' has several trailing spaces
name = RTrim( name )' After running the above code, name = "John Smith" and
address = RTrim( address )
' address = "21 Smithson Street"
Note that the RTrim function only removes trailing spaces (not leading spaces) from the supplied text string. If you wish to remove leading spaces, you will need to use the VBA LTrim or the VBA Trim function.