VBA TRIM Function (Syntax + Example)

Last Updated: June 22, 2023

- 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.




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


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:

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.


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