Thursday, August 11, 2016

How to use the TRIM Function (WS,VBA)

Description

The Microsoft Excel TRIM function returns a text value with the leading and trailing spaces removed. You can also use the TRIM function to remove unnecessary spaces between words in a string.

Syntax

The syntax for the TRIM function in Microsoft Excel is:
TRIM( text )

Parameters or Arguments

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

Applies To

  • Excel 2016, 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)

Example (as Worksheet Function)

Let's look at some Excel TRIM function examples and explore how to use the TRIM function as a worksheet function in Microsoft Excel:
Microsoft Excel
Based on the Excel spreadsheet above, the following TRIM examples would return:
=TRIM(A1)
Result: "Tech on the Net"

=TRIM(A2)
Result: "1234"

=TRIM(A3)
Result: "alphabet soup"

=TRIM(A4)
Result: "www.techonthenet.com"

=TRIM("  apples ")
Result: "apples"

Clean Up Spacing Between Words

You can also use the TRIM function in Excel to remove unnecessary spaces between words in a string. The TRIM function will reduce the spacing between words down to a single space.
Let's look at an example of how to use the TRIM function to clean up the spacing between words in Excel.
Microsoft Excel
Based on the spreadsheet above:
=TRIM(A1)
Result: "techonthenet.com is a great resource for Excel"

=TRIM("123 Main St.")
Result: "123 Main St."
In these two examples, the TRIM function will reduce the spacing between words down to a single space. This is a great way to clean up data that may not be formatted properly with unnecessary extra spaces.

Example (as VBA Function)

The TRIM function can also be used in VBA code in Microsoft Excel.
Let's look at some Excel TRIM function examples and explore how to use the TRIM function in Excel VBA code:
Dim LResult As String

LResult = Trim ("   Alphabet   ")
The variable LResult would now contain the value of "Alphabet".

No comments:

Post a Comment