Sub topic 1
Why Messy Text Causes Problems
Text that looks the same on screen is not always the same to Excel. A cell can contain a trailing space, a tab, or a character that the screen draws as a normal space but that Excel treats as something else. Excel compares what is stored in the cell, not what you see. This chapter explains the kinds of mess you will meet, how each one breaks common tasks, and how to confirm a problem before you fix it. The later chapters cover the fixes in detail.
Prerequisites
This chapter assumes you can already do the following:
- Enter a formula in a cell and copy it down a column.
- Read relative references such as
A2, and know why$E$2:$F$5stays fixed when you copy it. - Tell text from numbers in a cell. For example, you know that left-aligned
123is often text.
You don't need prior experience with TRIM, CLEAN, or PROPER.
The working model: Excel compares characters, not appearances
Every cell of text is a sequence of characters, and each character has a numeric code. A normal space is code 32. Two strings are equal only if every character matches, in the same order. The only exception is capitalization. Excel's = operator, lookup functions, COUNTIF, and Remove Duplicates all ignore upper and lower case.
That one rule explains most of the trouble in this chapter:
- An invisible extra character makes two strings different, even though they look identical.
- A difference in capitalization usually does not make them different. It still matters in places that do care about case, which are listed later.
The kinds of mess
Most messy text falls into four categories. Each has a typical source.
| Problem | What it looks like | Typical source |
|---|---|---|
| Extra spaces | Leading spaces, trailing spaces, or two spaces between words | Manual typing, fixed-width database exports that pad fields |
| Non-printing characters | Nothing visible, or small boxes in some fonts. Examples are tabs and line feeds, codes 0 to 31 | CSV and text files, system exports, copy and paste from other programs |
| Non-breaking spaces | Looks like a normal space, but is character 160 | Text copied from web pages, where HTML uses ` ` |
| Inconsistent capitalization | `smith`, `SMITH`, `sMith` in the same column | Several people typing, merged lists from different systems |
Line breaks inside a cell are a special case of non-printing characters. They are character 10, which you can also create by pressing Alt+Enter. They get their own chapter because removing them can join two words together.
The diagram below shows how the sources lead to the symptoms you will see.
A worked example
Suppose column A holds customer surnames from an import, and columns E and F hold a clean lookup table with customer IDs.
| Cell | Contents | `LEN` result |
|---|---|---|
| A2 | `Smith` followed by a trailing space | 6 |
| A3 | A leading space, then `Jones` | 6 |
| A4 | `NGUYEN` | 6 |
| A5 | `Patel` followed by a non-breaking space (character 160) | 6 |
| Cell | Contents |
|---|---|
| E2:F2 | `Smith`, 101 |
| E3:F3 | `Jones`, 102 |
| E4:F4 | `Nguyen`, 103 |
| E5:F5 | `Patel`, 104 |
In B2, enter this formula and copy it down to B5:
=VLOOKUP(A2,$E$2:$F$5,2,FALSE)The results are:
| Row | Result | Reason |
|---|---|---|
| 2 | `#N/A` | `Smith ` has a trailing space, so it is not equal to `Smith`. |
| 3 | `#N/A` | ` Jones` has a leading space. |
| 4 | 103 | `NGUYEN` matches `Nguyen`, because lookups ignore case. |
| 5 | `#N/A` | The trailing character is code 160, not a space. |
Rows 2, 3, and 5 look correct on screen and still fail. Row 4 looks wrong, because the capitalization differs, and still works.
How messy text breaks common tasks
Lookups and matches
VLOOKUP, XLOOKUP, MATCH, and INDEX/MATCH with exact matching all require identical text. A single hidden character on either side gives #N/A. This is the most common reason people discover a text problem. The usual first reaction is to retype the value, which can hide the cause and leaves the same fault in the other 5,000 rows.
Counting and comparing
COUNTIF and COUNTIFS also need identical text. With the data above, this formula returns 0, not 1:
=COUNTIF(A2:A5,"Smith")A direct comparison fails in the same way. =A2="Smith" returns FALSE. Any report built on these counts is wrong without showing an error, which is worse than #N/A.
Duplicates and grouping
Remove Duplicates and PivotTables group by exact text, apart from case. Smith and Smith become two separate items. A PivotTable then shows two rows for one customer, each with a partial total. Remove Duplicates keeps both, so your "deduplicated" list still has duplicates.
Sorting
A leading space sorts before any letter. A cell containing Jones therefore moves to the top of an A-to-Z sort, ahead of Adams. Sorting looks broken, but Excel is sorting the real contents correctly.
Numbers stored as text
An extra space makes a number into text. The cell 1234 with a trailing space is not a number. SUM skips it, and VLOOKUP will not match it against a numeric 1234. Text that has been cleaned can also stay text, so this problem has its own chapter.
Where capitalization does matter
Case differences rarely break Excel itself. They matter in these places:
EXACTandFINDare case-sensitive, unlikeSEARCH.- Databases, scripts, and other tools that receive your data may treat
smithandSmithas different values. - Reports and mailings look unprofessional with mixed case.
- A PivotTable shows the capitalization of the first occurrence it meets, which can be an odd one such as
sMITH.
Confirming the problem before you fix it
Don't assume a cell is clean. Test it. Three quick checks work on any cell. Put each in a spare column.
Compare the stored length with the length you expect:
=LEN(A2)Count how many characters TRIM would remove, which shows extra spaces:
=LEN(A2)-LEN(TRIM(A2))A result of 0 means no extra spaces of the normal kind. Any other number means the cell has leading, trailing, or doubled spaces.
Check whether a cell is unchanged by trimming. EXACT is case-sensitive, so it also catches capitalization if you compare against PROPER:
=EXACT(A2,TRIM(A2))If this returns FALSE, the cell contains extra spaces. If it returns TRUE but lookups still fail, suspect a character that TRIM does not touch, such as code 160. In that case, the following formula returns the code of the last character. A value of 160 means a non-breaking space.
=UNICODE(RIGHT(A2,1))In the example, A5 returns 160. A2 returns 32.
Which function fixes which problem
You will learn each function in its own chapter. This table is a map of what is ahead, including what each function leaves alone.
| Problem | Function | What it leaves alone |
|---|---|---|
| Extra spaces | `TRIM` | Non-breaking spaces (code 160) and non-printing characters |
| Non-printing characters, codes 0 to 31 | `CLEAN` | Spaces and non-breaking spaces |
| Inconsistent capitalization | `PROPER` | Spaces and hidden characters. It can also capitalize names wrongly |
| Non-breaking spaces and line breaks | `SUBSTITUTE` | Anything you do not name in the formula |
Two points are worth noting now:
TRIMremoves only the standard space, code 32. This is why a trailing non-breaking space survives it.CLEANremoves line feeds outright. Two words on separate lines can end up joined, such asNorthEast. A later chapter shows how to replace the line break with a space instead.
Clean in a copy, not in place
A cleaning formula returns a new value. It does not change the original cell. The standard method is to put the formula in a helper column beside the data, check the result, and only then replace the original values. This keeps your source data intact if a formula misbehaves. The next chapter sets up this workflow.
Common errors at this stage
- Retyping values to make a lookup work. This fixes one cell and leaves the rest of the column broken.
- Assuming
TRIMfixes everything. It handles only code 32. - Fixing case first.
PROPERleaves spaces alone, so the lookup still fails. - Cleaning only the data column. The lookup table can be the dirty side. Test both.
- Trusting
LENalone.LENcounts hidden characters, but it does not tell you which ones they are. UseUNICODEorCODEon a specific character to identify it.
Exercise
A VLOOKUP with exact matching returns #N/A for the value "Garcia" in A2, although the lookup table clearly contains "Garcia". =LEN(A2)-LEN(TRIM(A2)) returns 0. Name two possible causes and give one formula that would help identify the second.
Show answer
The first possible cause is that the lookup table's entry has extra spaces. The problem can be on either side, so test both cells. The second is a character that TRIM ignores, such as a non-breaking space (code 160) or a non-printing character such as a tab. =UNICODE(RIGHT(A2,1)) returns the code of the last character. A result of 160 shows a non-breaking space, and a result below 32 shows a control character. Comparing =LEN(A2) with the number of letters you can see also reveals a hidden extra character.
Exercise
Column A contains "Lee", "lee", and "LEE ". How many distinct items does a PivotTable built on column A show, and why?
Show answer
Two items. "Lee" and "lee" are grouped together, because PivotTable grouping ignores case. "LEE " has a trailing space, so it is a different text value and forms a separate item. This shows that spaces split groups, while capitalization alone does not.