Excel TRIM function: remove extra spaces from text
TRIM removes extra spaces. Learn why an invisible space makes VLOOKUP return #N/A and COUNTIF miscount, and how to fix it for good — with examples.
TRIM removes extra spaces from text: every space at the start and at the end, and all but one space between words. “ Karimov Aziz ” → “Karimov Aziz”.
The trouble is that a trailing space is invisible. A price list exported from accounting software, a table copied from a website, a name typed in a hurry — any of them can hide a space. To Excel, “Bread” and “Bread ” are two different texts. As a result VLOOKUP returns #N/A, COUNTIF undercounts and a PivotTable shows “Tashkent” twice.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Entered | TRIM result | Length before | Length after | |
| 2 | Karimov Aziz | Karimov Aziz | 14 | 12 | |
| 3 | Rahimova Dilnoza | Rahimova Dilnoza | 18 | 16 | |
| 4 | Tashkent | Tashkent | 10 | 8 | |
| 5 | Bread | Bread | 5 | 5 | |
| 6 |
Syntax
=СЖПРОБЕЛЫ(text)
- text — the cell to clean (
A2) or a formula result.
What TRIM does:
- removes all spaces at the start and end of the text;
- shrinks runs of spaces between words to one.
What it doesn’t do: it leaves the web “non-breaking space” (CHAR(160)) and line breaks alone — see the Pro section below. The result is always text.
Cleaning a column — step by step
1
Insert an empty column next to the dirty one. Select B2 and click Формулы → Текстовые → TRIM.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Entered | Clean | ||
| 2 | Karimov Aziz | 1 | ||
| 3 | Rahimova Dilnoza | |||
| 4 | ||||
| 5 |
2
Type A2 into the Text box and click OK — or type =СЖПРОБЕЛЫ(A2) into the cell. Then double-click the fill handle to copy it down.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Entered | Clean | ||
| 2 | Karimov Aziz | Karimov Aziz | ||
| 3 | Rahimova Dilnoza | Rahimova Dilnoza | ||
| 4 | Toshmatov Javohir | Toshmatov Javohir2 | ||
| 5 |
3
Copy column B (Ctrl + C), select A2 and paste with Вставить → Values. The original column is now clean; delete helper column B.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Entered | Clean | ||
| 2 | Karimov Aziz3 | Karimov Aziz | ||
| 3 | Rahimova Dilnoza | Rahimova Dilnoza | ||
| 4 | Toshmatov Javohir | Toshmatov Javohir | ||
| 5 |
The main trap: VLOOKUP can’t find what’s “there”
A real case: the price list was exported from accounting software and some product names end with a space. On the order sheet you write =ВПР(D2;$A$2:$B$4;2;ЛОЖЬ) — “Bread” is definitely in the list, yet the result is #N/A. Excel looks for “Bread”, but the list holds “Bread ”: a different text.
The dangerous part is that you can’t see this mistake. Many people rewrite the formula several times, convinced VLOOKUP is “broken”, while the culprit is a single space.
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Product (export) | Price | Lookup | Price | ||
| 2 | Bread 1 | 4 000 | Bread | #N/A | ||
| 3 | Milk | 12 000 | Milk | 12 000 | ||
| 4 | Yogurt2 | 9 000 | Yogurt | #N/A | ||
| 5 |
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Product (export) | Clean name | Price | |
| 2 | Bread | Bread | 4 000 | |
| 3 | Milk | Milk | 12 000 | |
| 4 | Yogurt | Yogurt | 9 000 | |
| 5 | ||||
| 6 | Lookup | Price | ||
| 7 | Bread | 4 000 | ||
| 8 | Yogurt | 9 000 | ||
| 9 |
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer | City | TRIM | |
| 2 | Aziz | Tashkent | Tashkent | |
| 3 | Dilnoza | Tashkent | Tashkent | |
| 4 | Javohir | Tashkent | Tashkent | |
| 5 | Malika | Samarkand | Samarkand | |
| 6 | Sardor | Tashkent | Tashkent | |
| 7 | Tashkent: | 1 | 4 | |
| 8 |
Pro level: a universal clean-up formula
For imported data, remove three kinds of junk in one formula:
=СЖПРОБЕЛЫ(CLEAN(ПОДСТАВИТЬ(A2;CHAR(160);" ")))
- SUBSTITUTE turns non-breaking spaces into normal ones;
- CLEAN removes invisible control characters, including line breaks inside a cell;
- TRIM removes the extra spaces.
Find dirty cells. Условное форматирование → New Rule → “Use a formula…” → =ДЛСТР(A2)<>ДЛСТР(СЖПРОБЕЛЫ(A2)) highlights cells with extra spaces. =СУММПРОИЗВ(--(ДЛСТР(A2:A500)<>ДЛСТР(СЖПРОБЕЛЫ(A2:A500)))) counts how many there are.
Clean inside the lookup. If the space is in the value you look up (say, on the order sheet), =ВПР(СЖПРОБЕЛЫ(D2);$A$2:$B$100;2;ЛОЖЬ) is enough — the table stays untouched.
Find a non-breaking space with Ctrl+H. In the “Find what” box hold Alt and type 0160 on the numeric keypad.
Try it in the simulator
Type =СЖПРОБЕЛЫ(A2) in B2 and fill it down to B5. Then type =ДЛСТР(A2)-ДЛСТР(B2) in C2 and fill it down too — see how many extra spaces were removed from each cell.
| A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Entered | Clean | Removed | |||||||||||||||||||||
| 2 | Karimov Aziz | |||||||||||||||||||||||
| 3 | Rahimova Dilnoza | |||||||||||||||||||||||
| 4 | Tashkent | |||||||||||||||||||||||
| 5 | Bread | |||||||||||||||||||||||
| 6 | ||||||||||||||||||||||||
| 7 | ||||||||||||||||||||||||
| 8 | ||||||||||||||||||||||||
| 9 | ||||||||||||||||||||||||
| 10 |
Has ExcelLab been useful?
Support the project and help us release new lessons and exercises.