How to use the VBA TRIM Function (Syntax + Example)

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

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 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 left and right both sides) from the cell A1.

Hey Listen,

This guide is dedicated to helping you to learn Visual Basic for Excel and start writing VBA codes and here we have some Examples of Macros (ready to use VBA codes).

Notes

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

Related Functions

FORMAT | INSTR | INSTRREV | LCASE | LEFT | LEN | LTRIM | MID | REPLACE | RIGHT | RTRIM | SPACE | STRCOMP | STRCONV | STRING | STRREVERSE | UCASE