VBA Trim function

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Trim Function Description

The VBA Trim function removes both leading and trailing whitespaces from a string, looking from both the left and right side of the string.

To remove only leading whitespaces see LTrim, to remove only trailing whitespaces see RTrim.

Syntax

The syntax for the Trim function in VBA is:

Trim( text )

Parameters

text
The string for which you wish to remove any leading and trailing (looking from the left and right) whitespaces from.

Other Notes

Trim does not remove non-whitespace characters such as new lines (vbNewLine, vbCrLf) etc.

Example usage

The Trim function can be used in VBA code. Let’s look at some VBA Trim function examples:

Trim "   Hello world!   "
'Result: "Hello world!"

Trim " " & vbNewLine & " Some Text " & vbNewLine & " "
'Result: vbNewLine & " Some Text " & vbNewLine 

Simply the best place to learn Excel VBA