tech on the net

MS Excel: TRIM Function (WS, VBA)

In Microsoft Excel, the TRIM function returns a text value with the leading and trailing spaces removed.

Syntax

The syntax for the TRIM function is:

TRIM( text )

text is the text value to remove the leading and trailing spaces from.

Applies To

Type of Function

Worksheet Function Example

Let's take a look at an example to see how you would use the TRIM function in a worksheet:

Microsoft Excel

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