VBA TRIM Function

HomeVBA Functions LIST (Category Wise)How to use the VBA TRIM Function (Syntax + Example)

What is VBA TRIM Function

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.

how to use it

To use VBA's TRIM function you need to understand its syntax and arguments:




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

Example to use TRIM Function in VBA

To practically understand how to use 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


Below are some important points which you need to take care while using TRIM function in VBA.

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

Related Functions

About the Author

puneet one point one

Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can find him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.