Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel String Commands: How to Clean and Manipulate Text

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Excel string commands, known as Text Functions, allow you to manipulate, clean, and reorganize text data within cells. You can achieve this by using formulas like LEFT, RIGHT, MID, CONCAT, and TRIM to extract specific characters, combine multiple cells, or remove unwanted spaces. These commands are essential for transforming messy data imports into professional, usable reports.

What You'll Need

  • Microsoft Excel (any modern version, including Office 365, Excel 2021, or Excel Online).
  • A dataset containing text strings (e.g., full names, email addresses, or product codes).

Step-by-Step

  1. Remove Extra Spaces with TRIM

    Often, data imported from other software contains leading, trailing, or double spaces that break other formulas. To fix this, click an empty cell and type =TRIM(A2) (assuming your text is in cell A2). Press Enter and drag the fill handle down to clean the entire column.

  2. Extract Text from the Start with LEFT

    Use the LEFT function when you need a specific number of characters from the beginning of a cell. For example, to extract a 3-letter area code from a phone number in cell A2, type =LEFT(A2, 3). The first argument is the cell, and the second is the number of characters to grab.

  3. Extract Text from the End with RIGHT

    Use the RIGHT function to pull characters from the end of a string. To extract a 4-digit year from the end of a string in cell A2, type =RIGHT(A2, 4). This is particularly useful for extracting file extensions or ID suffixes.

  4. Extract Text from the Middle with MID

    When the data you need is buried in the center of a cell, use MID. The syntax is =MID(text, start_num, num_chars). For example, if cell A2 contains "Order_12345_US" and you want the number "12345", type =MID(A2, 7, 5). This tells Excel to start at the 7th character and take the next 5 characters.

  5. Combine Text with CONCAT or Ampersand

    To join two or more strings together, you can use the CONCAT function or the & symbol. To combine a first name in A2 and a last name in B2 with a space between them, type =A2 & " " & B2 or =CONCAT(A2, " ", B2). Always put manual text (like spaces or commas) inside double quotation marks.

  6. Change Case with UPPER, LOWER, and PROPER

    To standardize the look of your text, use case functions. =UPPER(A2) makes all text uppercase; =LOWER(A2) makes it all lowercase; and =PROPER(A2) capitalizes the first letter of every word, which is ideal for names.

  7. Find and Replace Specific Text with SUBSTITUTE

    To swap one piece of text for another within a cell, use =SUBSTITUTE(A2, "old_text", "new_text"). For example, to change all dashes to underscores in cell A2, type =SUBSTITUTE(A2, "-", "_").

Common Mistakes

  • Forgetting Quotation Marks: Excel treats text inside a formula as a named range or a function unless it is enclosed in double quotes. =LEFT(A2, 3) & space will cause an error; =LEFT(A2, 3) & " " will work.
  • Hard-coding Lengths: Using a fixed number in MID or LEFT can fail if your data varies in length. For example, if some names are 5 letters and some are 10, a fixed LEFT(A2, 5) will cut off longer names.
  • Ignoring Hidden Spaces: If a formula like =A2="Yes" returns FALSE even though the cell looks like it says "Yes", there is likely a hidden space. Always use TRIM on your source data first.
  • Applying Formulas to the Source Cell: You cannot put a formula in cell A2 that references cell A2; this creates a circular reference error. Always put your string commands in a new, adjacent column.

Tips

  • Nest Your Functions: You can combine commands. For example, =PROPER(TRIM(A2)) will remove extra spaces and capitalize the names in one single step.
  • Use FIND for Dynamic Extraction: Instead of guessing the number of characters for MID, use the FIND function to locate a specific character (like a comma or space) and use that number as your starting point.
  • Convert Formulas to Values: Once you have cleaned your text, the cells still contain formulas. To keep the text but remove the formula, copy the cells, right-click, and select Paste Special > Values.
  • Flash Fill: For simple patterns, try typing the desired result in the cell next to your data and pressing Ctrl + E. Excel will attempt to recognize the pattern and fill the rest automatically.

Troubleshooting

If your string commands are not producing the expected results, try these steps in order:

  1. Check for #VALUE! Errors: This usually happens if you provide a number where Excel expects text, or vice versa. Ensure your cell references are correct.
  2. Verify Character Counts: Remember that spaces, commas, and periods count as characters. If your MID function is off by one, double-check if you counted the spaces in the string.
  3. Check Formula Syntax: Ensure every opening parenthesis ( has a matching closing parenthesis ). Excel will usually highlight the matching pair in color.
  4. Inspect Data for Non-Breaking Spaces: Some web data contains "non-breaking spaces" that the standard TRIM function cannot remove. If TRIM fails, use the Find and Replace tool (Ctrl + H) to manually replace those specific characters.

Frequently Asked Questions

Can I split a cell into multiple columns without formulas?

Yes. Select your data, go to the Data tab, and select "Text to Columns." This allows you to split text based on a delimiter like a comma or a tab.

How do I remove all numbers from a text string?

Standard string commands cannot easily remove all numbers. This typically requires a complex array formula or the use of a User Defined Function (UDF) via VBA, or the "Flash Fill" feature.

What is the difference between CONCAT and TEXTJOIN?

CONCAT simply joins strings together. TEXTJOIN is more powerful because it allows you to specify a delimiter (like a comma) and choose whether to ignore empty cells automatically.

Why is my LEFT function returning a number instead of text?

The LEFT function always returns a text string. If the result looks like a number but behaves like text (e.g., it is left-aligned), you can wrap the formula in VALUE(), like =VALUE(LEFT(A2, 3)), to convert it back to a number.

Does the MID function count from 0 or 1?

Excel string functions are 1-indexed, meaning the first character of the cell is position 1, not 0.