Excel tutorial
/

Functions

Beginner

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.

Книга1 - Excel
—☐✕
B2fx=СЖПРОБЕЛЫ(A2)
ABCDE
1EnteredTRIM resultLength beforeLength after
2 Karimov AzizKarimov Aziz1412
3Rahimova Dilnoza Rahimova Dilnoza1816
4 Tashkent Tashkent108
5BreadBread55
6
Готово
You can barely see a difference, but LEN exposes the hidden spaces

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.

Книга1 - Excel
—☐✕
ФайлГлавнаяВставкаРазметка страницыФормулыДанныеРецензированиеВидСправка
Вставить функциюАвтосуммаФинансовыеЛогическиеТекстовыеДата и времяСсылки и массивыМатематические
Библиотека функций
Диспетчер имён
Определённые имена
B2fx
ABCD
1EnteredClean
2 Karimov Aziz1
3Rahimova 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.

Книга1 - Excel
—☐✕
B2fx=СЖПРОБЕЛЫ(A2)
ABCD
1EnteredClean
2 Karimov AzizKarimov Aziz
3Rahimova Dilnoza Rahimova Dilnoza
4 Toshmatov JavohirToshmatov Javohir2
5
ГотовоКоличество: 3

3

Copy column B (Ctrl + C), select A2 and paste with Вставить → Values. The original column is now clean; delete helper column B.

Книга1 - Excel
—☐✕
ФайлГлавнаяВставкаРазметка страницыФормулыДанныеРецензированиеВидСправка
Вставить
Буфер обмена
Calibri ▾11 ▾
Шрифт
Выравнивание
Общий ▾
Число
Условное форматированиеФорматировать как таблицуСтили ячеек
Стили
ВставитьУдалитьФормат
Ячейки
АвтосуммаСортировка и фильтрНайти и выделить
Редактирование
A2fx Karimov Aziz
ABCD
1EnteredClean
2 Karimov Aziz3Karimov Aziz
3Rahimova Dilnoza Rahimova Dilnoza
4 Toshmatov JavohirToshmatov Javohir
5
ГотовоКоличество: 3

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.

Книга1 - Excel
—☐✕
E3fx=ВПР(D3;$A$2:$B$4;2;ЛОЖЬ)
ABCDEF
1Product (export)PriceLookupPrice
2Bread 14 000Bread#N/A
3Milk12 000Milk12 000
4 Yogurt29 000Yogurt#N/A
5
Готово
Only “Milk” is found: A2 ends with a hidden space and A4 starts with one, so E2 and E4 show #N/A
Книга1 - Excel
—☐✕
B7fx=ВПР(A7;$B$2:$C$4;2;ЛОЖЬ)
ABCD
1Product (export)Clean namePrice
2Bread Bread4 000
3MilkMilk12 000
4 YogurtYogurt9 000
5
6LookupPrice
7Bread4 000
8Yogurt9 000
9
Готово
The fix: build a clean-name column with TRIM and let VLOOKUP search it (B:C). Now everything is found
Книга1 - Excel
—☐✕
C7fx=СЧЁТЕСЛИ(C2:C6;"Tashkent")
ABCD
1CustomerCityTRIM
2AzizTashkentTashkent
3DilnozaTashkent Tashkent
4Javohir TashkentTashkent
5MalikaSamarkandSamarkand
6SardorTashkent Tashkent
7Tashkent:14
8
Готово
COUNTIF is fooled too: only 1 “Tashkent” in the original column, 4 after cleaning. PivotTables and Remove Duplicates make the same mistake

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.

Книга1 - Excel
—☐✕
Буфер обмена
Шрифт
Выравнивание
Число
Стили
Ячейки
Редактирование
✕✓fx
ABCDEFGHIJKLMNOPQRSTUVWX
1EnteredCleanRemoved
2 Karimov Aziz
3Rahimova Dilnoza
4 Tashkent
5Bread
6
7
8
9
10
Лист1
Готово

Has ExcelLab been useful?

Support the project and help us release new lessons and exercises.

Support us
Previous

112 / 154