MS Excel: TRIM Function (WS, VBA)
In Microsoft Excel, the TRIM function returns a text value with the leading and trailing spaces removed.
The syntax for the TRIM function is:
TRIM( text )
text is the text value to remove the leading and trailing spaces from.
- Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000
Type of Function
- Worksheet function (WS)
- VBA function (VBA)
Worksheet Function Example
Let's take a look at an example to see how you would use the TRIM function in a worksheet:
Based on the Excel spreadsheet above, the TRIM function would return the following:
|=TRIM(A1)||would return "Tech on the Net"|
|=TRIM(A2)||would return "1234"|
|=TRIM(A3)||would return "alphabet soup"|
|=TRIM(A4)||would return "www.techonthenet.com"|
|=TRIM(" apples ")||would return "apples"|
VBA Function Example
The TRIM function can also be used in VBA code. For example:
Dim LResult As String LResult = Trim (" Alphabet ")
The variable LResult would now contain the value of "Alphabet".