ZetCode

Excel TRIM Function

last modified April 4, 2025

The TRIM function is an essential text manipulation tool in Excel. It removes extra spaces from text, leaving only single spaces between words. This tutorial provides a comprehensive guide to using the TRIM function with detailed examples. You'll learn basic syntax, practical applications, and advanced techniques to clean text data in Excel.

TRIM Function Basics

The TRIM function removes all spaces from text except for single spaces between words. It's particularly useful for cleaning data imported from other systems. The syntax is simple but powerful for text processing.

Component Description
Function Name TRIM
Syntax =TRIM(text)
Arguments text - The text to clean
Return Value Text with normalized spacing

This table breaks down the essential components of the TRIM function. It shows the function name, basic syntax format, required argument, and return value characteristics.

Basic TRIM Example

This example demonstrates the simplest use of the TRIM function with a text string containing extra spaces.

Basic TRIM formula
=TRIM("   Excel    TRIM   function   ")

This formula processes the text string, removing leading, trailing, and redundant spaces. The result will be "Excel TRIM function" with single spaces.

TRIM with Cell References

A more practical use of TRIM involves cleaning text from specific cells. Here's an example with cell references.

A B
Data cleaning
=TRIM(A1)

The table shows a simple spreadsheet with text containing extra spaces in cell A1 and a TRIM formula in cell B1 that cleans the text.

TRIM with cell reference
=TRIM(A1)

This formula processes the text in cell A1, removing all leading, trailing, and multiple internal spaces. The result will be "Data cleaning".

TRIM with Non-Breaking Spaces

TRIM doesn't remove non-breaking spaces (CHAR(160)) by default. This example shows how to handle such cases with a combined formula.

A B
Report  Data
=TRIM(SUBSTITUTE(A1, CHAR(160), " "))

This table demonstrates handling text with non-breaking spaces (represented by  ). The formula first converts these to regular spaces, then trims.

TRIM with non-breaking spaces
=TRIM(SUBSTITUTE(A1, CHAR(160), " ")

This formula first replaces non-breaking spaces with regular spaces using SUBSTITUTE, then applies TRIM. The result is clean text with single spaces.

TRIM with Concatenated Text

When combining text from multiple sources, TRIM helps clean the results. This example shows TRIM with CONCATENATE.

A B C
John Doe
=TRIM(CONCATENATE(A1, " ", B1))

The table illustrates how TRIM can clean up text combined from multiple cells that may contain extra spaces. The CONCATENATE joins first and last names.

TRIM with CONCATENATE
=TRIM(CONCATENATE(A1, " ", B1))

This formula combines first and last names with a space, then trims the result. Without TRIM, the result would have extra spaces between the names.

TRIM in Data Validation

TRIM is useful in data validation to ensure consistent text entry. This example shows a validation rule using TRIM.

Data validation with TRIM
=LEN(TRIM(A1))=LEN(A1)

This validation formula ensures entered text contains no leading, trailing, or multiple spaces. It compares the length of original and trimmed text.

TRIM with Other Functions

TRIM often works with other text functions for comprehensive cleaning. This example combines TRIM with PROPER for formatted names.

A B
john SMITH
=PROPER(TRIM(A1))

The table shows how to combine TRIM with PROPER to clean and format names consistently. TRIM removes extra spaces before PROPER capitalizes the text.

TRIM with PROPER
=PROPER(TRIM(A1))

This formula first removes extra spaces with TRIM, then applies PROPER to format the name correctly. The result is "John Smith" from the messy input.

The TRIM function is essential for cleaning and standardizing text data in Excel. From simple space removal to complex data preparation workflows, TRIM handles it all efficiently. Mastering its various applications will significantly improve your text processing capabilities. Remember that TRIM doesn't affect non-breaking spaces by default.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Excel Formulas.