Excel Function: TRIM

Purpose

The TRIM function operates on a text value by replacing sequences of 2 or more spaces with a single space and removing leading and trailing spaces.

Its output is always a value of type text.

Invocations of the TRIM function take the form:

TRIM(text)

Limitations

Arguments

ArgumentArgument typeDescription
textMandatoryA text value whose trailing & leading spaces will be trimmed.

Related Functions

Function(s)ExamplePurpose

Error Conditions

Error valueCondition typeCondition
#VALUE!Bad argumentA value of type currency, geography, or stock was supplied to the text argument.

Examples

ExampleFormulaResultDescription
1=TRIM(" a b c d ")a b c dSingle spaces between characters are preserved, leading & trailing spaces are trimmed.
2=TRIM(" a      b c       d ")a b c dSequences of 2 or more spaces between characters are replaced with a single space.
3=TRIM(DATE(2023,5,7))45053The date May 7, 2023 is stored as the number 45053. It’s converted to text and returned.
4=TRIM(TIME(12,0,0))0.5The time 12:00:00 is stored as 0.5. It’s converted to text and returned.
5=TRIM(1)1Numbers are treated as text.

Examples: Errors

ExampleValue supplied to “text” argumentResultDescription
1MICROSOFT CORPORATION (XNAS:MSFT)#VALUE!Supplying a stock value to the TRIM function causes an error.
2USD/EUR#VALUE!Supplying a currency value to the TRIM function causes an error.
3Melbourne#VALUE!Supplying a geography value to the TRIM function causes an error.

These errors can be mitigated by converting values of type stock, currency and geography to text before passing them to the TRIM function. I.e.:

TRIM(VALUETOTEXT(text,0))