Sub topic 4
Trim with a formula: the TRIM function
This chapter assumes you can enter simple formulas such as =A1&B1 and select ranges. The previous chapter used the Trim whitespace menu tool, which edits cells in place. The TRIM function does the same job in a formula. It leaves your original data untouched and writes the cleaned text somewhere else.
What TRIM does
TRIM takes one text value and returns it with the extra spaces removed. It makes three changes:
- It removes spaces at the start of the text.
- It removes spaces at the end of the text.
- It reduces any run of spaces between words to a single space.
=TRIM(A2)If A2 contains " Jane Doe ", the formula returns "Jane Doe".
The formula takes a single argument. That argument can be a cell reference, a text string, or the result of another function.
Why use a formula when the menu tool exists
Both approaches remove the same spaces. They differ in how they behave:
| Aspect | Trim whitespace tool | TRIM function |
|---|---|---|
| Changes the original cells | Yes, in place | No, output goes to other cells |
| Updates when source data changes | No, you must run it again | Yes, recalculates automatically |
| Works as a step in a longer formula | No | Yes |
| Result is | Plain values | Formula results, until you convert them |
Use the tool for a one-time fix on a copy of the data. Use the function when new rows keep arriving, for example from a form or an import. Use it as well when you want a cleaned column feeding another formula such as VLOOKUP, UNIQUE, or SPLIT.
Why stray spaces matter
Sheets compares text exactly. The values "Acme" and "Acme " (with a trailing space) are different strings, and the extra space is invisible when you look at the cell. This causes several problems:
- A
VLOOKUPorMATCHfor"Acme"returns#N/Awhen the table holds"Acme ". COUNTIFcounts the two spellings separately.- Pivot tables list them as two separate groups.
- The Remove duplicates tool treats them as different rows.
A quick way to detect the problem is to compare lengths:
=LEN(A2) - LEN(TRIM(A2))The result is the number of characters TRIM would remove. A result of 0 means the cell has no extra spaces. You can also test a whole cell with =EXACT(A2, TRIM(A2)), which returns FALSE when trimming would change anything.
A worked example
Suppose column A holds company names exported from another system.
| Row | A (raw) | B (`=TRIM(A2)` filled down) | C (`=LEN(A2)-LEN(B2)`) |
|---|---|---|---|
| 2 | ` Acme Corp` | `Acme Corp` | 2 |
| 3 | `Globex Inc ` | `Globex Inc` | 3 |
| 4 | `Initech` | `Initech` | 0 |
Follow these steps:
- Leave column A alone. This is your raw data.
- In B2, enter
=TRIM(A2). - Drag the fill handle down, or double-click it, to copy the formula along the column.
- In C2, enter the length-difference check to see which rows changed.
Now a lookup such as =VLOOKUP(B2, Prices!A:B, 2, FALSE) can match names in the other sheet, as long as those names are clean too.
Cleaning a whole column with one formula
Filling a formula down works, but new rows need the formula copied again. ARRAYFORMULA lets one formula handle the entire column:
=ARRAYFORMULA(TRIM(A2:A))Place this in B2 and nothing else in column B below it. The results spill down automatically. Rows where A is empty return empty text, so the output looks blank.
Two details matter here:
- Without
ARRAYFORMULA,TRIM(A2:A)returns only the first result. Sheets does not apply a plainTRIMacross a range on its own. - Spilling needs free space. If any cell below B2 already contains data, you get a
#REF!error that says the array result was not expanded. Clear those cells.
The open-ended range A2:A reaches the bottom of the sheet. That is convenient for growing data. In a very large sheet it also makes Sheets evaluate many empty rows, so use a bounded range such as A2:A5000 if recalculation feels slow.
Making the result permanent
Formula output depends on the formula. If you delete column A, the cleaned column breaks. To keep only the cleaned text:
- Select the formula cells.
- Copy them with Ctrl+C (Cmd+C on Mac).
- Choose Edit > Paste special > Values only, or press Ctrl+Shift+V.
You can paste the values over the formulas in column B. You can also paste them into a different sheet or into a copy of the original column. Do not paste over your raw column unless you kept a backup. The earlier chapter on protecting the original explains why.
What TRIM does not remove
This is the most common reason TRIM appears to do nothing. It removes only the standard space character, which has the character code 32. It leaves these alone:
- Non-breaking spaces (code 160), common in text copied from web pages. They look like spaces but are a different character.
- Line breaks (
CHAR(10)) inside a cell. Cells containing them keep their line breaks after trimming. - Tabs (
CHAR(9)) and other non-printing characters.
You can test a cell for a non-breaking space with =ISNUMBER(FIND(CHAR(160), A2)). The next chapter covers CLEAN and SUBSTITUTE, which handle these cases. For now, remember that if LEN(A2) and LEN(TRIM(A2)) are equal but the cell still looks padded, a different character is probably responsible.
Data types: numbers and dates
TRIM is a text function, and it can change how a value behaves in a cell. Consider an ID that was imported as " 00123 ". After TRIM, you get "00123". This is text, and the leading zeros are preserved. That is often what you want for IDs and postal codes.
Now consider a number that was imported as text, such as " 42 ". After trimming you get the text "42". It may still behave as text in a lookup against real numbers. Convert it explicitly when you need a number:
=VALUE(TRIM(A2))VALUE returns an error if the text is not a valid number, which makes it a useful check for bad entries. Do not wrap IDs with leading zeros in VALUE, because the zeros disappear.
Combining TRIM with other functions
TRIM works well as the innermost layer of a longer formula, because later functions then see tidy input. Here are three common patterns:
=LOWER(TRIM(A2))
=SPLIT(TRIM(A2), ",")
=ARRAYFORMULA(IF(A2:A = "", "", TRIM(A2:A)))The first normalizes capitalization after trimming. The second prevents extra empty-looking pieces when you split text. The third returns a truly empty cell for blank rows instead of empty text. Use it when a later step needs to tell blank rows from text.
A related pitfall: when you build a key by joining columns, trim each part before you join them.
=TRIM(A2) & "|" & TRIM(B2)If you trim only the combined result, spaces around the | separator stay put, and keys that should match may not.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Output is identical to input | The cell holds non-breaking spaces or line breaks | Use `CLEAN` and `SUBSTITUTE` (next chapter) |
| Only one result appears | `ARRAYFORMULA` is missing | Wrap the formula in `ARRAYFORMULA` |
| `#REF!` when spilling | Cells below the formula are not empty | Clear the cells in the way |
| Lookups still fail after trimming | The lookup key column is not trimmed | Trim both sides of the comparison |
| Numbers will not add up | Trimmed values are still text | Wrap with `VALUE` |
| Cleaned column breaks after deleting the source | The cells are still formulas | Paste values only |
Exercises
Exercise
Column A contains the text " North West ". Write a formula for B2 that returns "North West", and a second formula for C2 that returns how many characters were removed.
Show answer
In B2 enter =TRIM(A2). In C2 enter =LEN(A2)-LEN(B2) (or =LEN(A2)-LEN(TRIM(A2))). The original text has 2 leading spaces, 3 spaces between the words, and 2 trailing spaces, which is 7 spaces of padding that become 1 space. The cleaned text has 1 space between the words, so 5 characters are removed.
Exercise
You enter =TRIM(A2:A100) in B2 and only B2 shows a value. Then you fix it, but a #REF! error appears because B10 contains old data. What did you change, and what must you do about B10?
Show answer
Wrap the formula in ARRAYFORMULA: =ARRAYFORMULA(TRIM(A2:A100)). Plain TRIM does not apply across a range. The #REF! appears because an array result needs empty cells to spill into, so clear B10 (and any other occupied cells below B2 in that range).
Exercise
A cell looks like it has a leading space, and =TRIM(A2) returns the same visible text. LEN(A2) and LEN(TRIM(A2)) are equal. What is the most likely cause, and how can you test for it?
Show answer
The leading character is probably a non-breaking space (CHAR(160)), which TRIM does not remove. Test with =ISNUMBER(FIND(CHAR(160), A2)); TRUE confirms it. Replace it with a normal space using SUBSTITUTE before trimming, as covered in the next chapter.