VBA TRIM Function (Syntax + Example)

Last Updated: June 22, 2023
puneet-gogia-excel-champs

- Written by Puneet

The VBA TRIM function is listed under the text category of VBA functions. When you use it in a VBA code, it removes the leading and trailing spaces from the value supplied. In simple words, if there’s a space before and after the value it will remove it and returns the rest of the value in the result.

Syntax

Trim(String)

Arguments

  • String: A text string from which you want to remove the leading and trailing spaces.

Example

To practically understand how to use the VBA TRIM function, you need to go through the below example where we have written a vba code by using it:

example-vba-trim-function
Sub example_TRIM()
Range("B1").Value = Trim(Range("A1"))
End Sub

In the above code, we have used the TRIM to remove all the unwanted spaces (from the left and right sides) from cell A1.

Notes

  • If the supplied string is NULL, then it will return NULL in the result.