Sub topic 1
Why messy text causes problems
Text that looks the same on screen can differ underneath. Excel compares the characters stored in cells, not what you see. Extra spaces, hidden control characters, and inconsistent capitalization cause wrong results that give no error message. This chapter describes those problems and shows how to detect them. The next chapters cover the fixes.
Prerequisites
This chapter assumes you can do the following:
- Enter a formula and copy it down a column.
- Read a relative reference such as
A2. - Tell a number from text in a cell. Text is left-aligned by default, and numbers are right-aligned.
The three common problems
Imported and hand-typed text usually has one or more of these problems.
| Problem | Typical source | What you see |
|---|---|---|
| Extra spaces | Hand typing, exports with padded fields | Usually nothing, or slightly odd spacing |
| Hidden characters | Web pages, databases, PDFs, other systems | Nothing, or a line break inside a cell |
| Inconsistent capitalization | Several people entering data | "smith", "SMITH", "Smith" |
Excel has one function for each problem:
TRIMhandles extra spaces.CLEANhandles non-printing characters.PROPERhandles capitalization.
Later chapters cover each function in detail.
How the problems break your work
Comparisons fail
Suppose A2 holds Smith and B2 holds Smith with a trailing space. This formula returns FALSE:
=A2=B2Lookup functions such as VLOOKUP, XLOOKUP, and MATCH compare cells the same way. A lookup for Smith returns #N/A when the source table stores Smith with a trailing space. COUNTIF and COUNTIFS simply return too low a count. The same applies to duplicate removal: Smith and Smith count as two different values.
Sorting looks wrong
A leading space sorts before any letter. A name such as Adams can therefore appear at the top of an A to Z sort, away from the other A names.
Capitalization is cosmetic in most Excel features
Excel's = operator, VLOOKUP, COUNTIF, and sorting ignore case. smith and SMITH match in all of them. Inconsistent capitalization mainly hurts reports, mail merges, and any system that you export the data to and that is case-sensitive. The EXACT function is also case-sensitive.
Hidden characters are the hardest to spot
A tab, a line feed, or a non-breaking space produces the same failures as an extra space, but nothing visible shows it. You can't fix a problem you can't see, so you need to detect it first.
Spotting a problem before you fix it
Compare the length of a cell with its length after TRIM:
=LEN(A2)-LEN(TRIM(A2))A result of 0 means TRIM has nothing to remove. Any other number is the count of characters TRIM would remove. Fill the formula down a helper column next to your data and filter for values above 0.
If the result is 0 but a lookup still fails, a hidden character that TRIM doesn't remove may be present. This formula returns the character code of the last character in the cell:
=UNICODE(RIGHT(A2,1))A normal space returns 32. A value of 160 means a non-breaking space, which TRIM leaves in place. A later chapter covers that case.
Practice
Exercise
A2 contains "Jones" and B2 contains "Jones " with one trailing space. What does =A2=B2 return, and what does =LEN(B2)-LEN(TRIM(B2)) return?
Show answer
=A2=B2 returns FALSE, because the trailing space makes the two strings different. =LEN(B2)-LEN(TRIM(B2)) returns 1, because TRIM would remove one character.
Exercise
A lookup for "Lee" fails, but =LEN(A2)-LEN(TRIM(A2)) returns 0 for the matching cell. Which formula checks whether the last character is a non-breaking space, and which value confirms it?
Show answer
=UNICODE(RIGHT(A2,1)) confirms it by returning 160.