TRIM Function in Excel

💡 Part 1: Introduce

📌 Definition: The TRIM function in Microsoft Excel is a text function that removes extra spaces from a given text string, including leading, trailing, and multiple consecutive spaces, leaving only a single space between words.

🎯 Purpose: The purpose of the TRIM function is to clean up the text by eliminating unnecessary spaces that can cause inconsistencies or issues when performing data analysis, comparisons, or formatting operations.

📚 Syntax & Arguments: The syntax of the TRIM function is as follows:

syntax
=TRIM(text)
  • text: This is the required argument representing the text string or cell reference from which you want to remove extra spaces.

🔍 Explain the Arguments in the function:

  • text: It can be either a text string enclosed in double quotation marks (“”) or a reference to a cell containing the text you want to trim.

💡 Return Value: The TRIM function returns a new text string with all extra spaces removed, leaving only a space between words.

📝 Remarks:

  • The TRIM function removes only spaces (ASCII code 32) from the text. It does not extract other non-printable characters or special characters.
  • If a text string contains tab characters or other non-printable characters, you may need to use additional functions, such as CLEAN or SUBSTITUTE, combined with TRIM to achieve the desired result.

Part 2: Examples

Let’s explore three examples that demonstrate the usage of the TRIM function:

1️⃣ Example 1: Cleaning Product Names

AB
1Product NameCleaned Product Name
2Widget=TRIM(A2)
3Tool=TRIM(A3)
4Pen=TRIM(A4)

In this example, we have a list of product names in column A. We use the TRIM function in column B to remove any extra spaces from the characters. The formula =TRIM(A2) in cell B2 removes any leading or trailing spaces from the product name “Widget.” Similarly, the names “Tool” and “Pen” in cells A3 and A4 are also cleaned using the TRIM function.

2️⃣ Example 2: Removing Spaces in Codes

AB
1Product CodeCleaned Product Code
2ABC 123=TRIM(SUBSTITUTE(A2,” “,””))
3XYZ 789=TRIM(SUBSTITUTE(A3,” “,””))
4PQR 456=TRIM(SUBSTITUTE(A4,” “,””))

In this example, we have a list of product codes in column A that contain spaces. We want to remove the holes from the regulations. The formula =TRIM(SUBSTITUTE(A2," ","")) in cell B2 removes the areas using the SUBSTITUTE function and then applies the TRIM function to remove any additional areas that might be present. The cleaned product codes are displayed in column B.

3️⃣ Example 3: Cleaning Address Data

AB
1AddressCleaned Address
2123 Main St=TRIM(SUBSTITUTE(A2,” “,” “))
345 Park Ave=TRIM(SUBSTITUTE(A3,” “,” “))
4789 Elm Rd=TRIM(SUBSTITUTE(A4,” “,” “))

In this example, we have a list of addresses in column A. The lessons may contain multiple consecutive spaces between words. We want to clean up the talks by removing extra spaces and leaving only a single space between words. The formula =TRIM(SUBSTITUTE(A2," "," ")) in cell B2 uses the SUBSTITUTE function to replace multiple consecutive areas with a single room. Then it applies the TRIM function to remove any remaining leading or trailing spaces. The cleaned addresses are displayed in column B.

4️⃣ Example 4: Extracting Initials

AB
1Full NameInitials
2John Doe=TRIM(LEFT(A2,1) & “.” & MID(A2,FIND(” “,A2)+1,1) & “.”)

In this example, we have a list of full names in column A. We want to extract the initials from each name. The formula =TRIM(LEFT(A2,1) & "." & MID(A2,FIND(" ",A2)+1,1) & ".") in the cell, B2 uses the LEFT function to extract the first letter of the first name, concatenates it with a period, uses the MID function along with FIND to extract the first letter of the last name, and appends it with a period. The result is the initials “J. D.”

5️⃣ Example 5: Removing Line Breaks

AB
1DescriptionCleaned Description
2This is a test. Line1 Line2=TRIM(SUBSTITUTE(A2,CHAR(10),” “))

In this example, we have column A descriptions containing line breaks represented by the character CHAR(10). We want to remove the line breaks and replace them with spaces. The formula =TRIM(SUBSTITUTE(A2,CHAR(10)," ")) in cell B2 uses the SUBSTITUTE function to replace the line break with space and applies the TRIM function to remove any leading or trailing spaces. The cleaned description is displayed in cell B2.

6️⃣ Example 6: Formatting Phone Numbers

AB
1Phone NumberFormatted Phone Number
2(123) 456-7890=TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,”(“,””),”)”,””),”-“,””))

In this example, we have phone numbers in column A with parentheses and dashes. We want to format them by removing the extra characters. The formula =TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"(",""),")",""),"-","")) cell B2 uses nested SUBSTITUTE functions to remove the parentheses and dashes and applies the TRIM function to remove any leading or trailing spaces. The formatted phone number is displayed in cell B2.

7️⃣ Example 7: Cleaning Employee IDs

AB
1Employee IDCleaned Employee ID
2emp 123=TRIM(RIGHT(A2,LEN(A2)-FIND(” “,A2)))

In this example, we have employee IDs in column A that contain an extra space before the ID. We want to clean up the IDs by removing the leading area. The formula =TRIM(RIGHT(A2,LEN(A2)-FIND(" ",A2))) in cell B2 uses the FIND function to locate the area’s position, calculates the ID’s length excluding the space, and uses the RIGHT function to extract the ID. The TRIM function then removes any leading or trailing spaces. The cleaned employee ID is displayed in cell B2.

8️⃣ Example 8: Removing Non-Numeric Characters

AB
1ValueNumeric Only
2$1,234.56=TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,”$”,””),”,”,””),”-“,””),” “,””))

In this example, we have values in column A that contain currency symbols, commas, dashes, and spaces. We want to extract only the numeric portion of the values. The formula =TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"$",""),",",""),"-","")," ","")) cell B2 uses nested SUBSTITUTE functions to remove the currency symbol, commas, dashes, and spaces and applies the TRIM function to remove any leading or trailing spaces. The result is the numeric value “1234.56.”

9️⃣ Example 9: Extracting Email Domains

AB
1Email AddressDomain Name
2[email protected]=TRIM(RIGHT(A2,LEN(A2)-FIND(“@”,A2)))

In this example, we have email addresses in column A. We want to extract the domain name from each lesson. The formula =TRIM(RIGHT(A2,LEN(A2)-FIND("@",A2))) in cell B2 uses the FIND function to locate the position of the “@” symbol, calculates the length of the address excluding the “@” symbol, and uses the RIGHT function to extract the domain name. The TRIM function then removes any leading or trailing spaces. The domain name is displayed in cell B2.

🔟 Example 10: Removing Non-Alphanumeric Characters

AB
1TextAlphanumeric Only
2This is 1 a test!=TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,” “,””),”.”,””),”,”,””),”!”,””),”?”,””))

In this example, we have text strings in column A that contain spaces, periods, commas, exclamation marks, and question marks. We want to remove all non-alphanumeric characters and clean up the text. The formula =TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2," ",""),".",""),",",""),"!",""),"?","")) in cell B2 uses nested SUBSTITUTE functions to remove the unwanted feelings and applies the TRIM function to remove any leading or trailing spaces. The result is the cleaned text “Thisis1atest.”

Part 3: Tips and Tricks

  1. The TRIM function is handy when working with imported data containing leading or trailing spaces inadvertently added during data entry or extraction.
  2. If you need to remove spaces from the middle of a text string, combine the TRIM function with other functions like SUBSTITUTE or REPLACE to achieve the desired result.
  3. The TRIM function is not case-sensitive, meaning it treats uppercase and lowercase letters similarly when removing spaces. It only focuses on space characters and does not affect the case of the text.