VBA Function: RTrim


The VBA RTRIM Function returns a string after removing spaces from the right of the string.

RTRIM Syntax


VBA RTrim Example

Remove spaces from the right side of a string only:

Sub RTrimExample()

    MsgBox RTrim("   test")           'Returns : "   test"
    MsgBox RTrim("test   ")           'Returns : "test"
    MsgBox RTrim("   test   ")        'Returns : "   test"
    MsgBox RTrim("   test   test   ") 'Returns : "   test   test"
End Sub
To remove spaces from the left side of the string only, use the LTRIM function.
To remove extraneous spaces from the beginning and end of the string, use the TRIM function.