Sub topic 4
Numbers stored as text and hidden spaces
This chapter explains why a cell can look like a number and still behave like text. It also shows how to find the cause and convert the data. It assumes you can enter and edit formulas, use cell references and ranges such as A2 and A2:A10, and use SUM, AVERAGE and VLOOKUP. It also assumes you know that Excel treats numbers and text as different types of value.
Why this causes errors
Excel stores every cell value as a type. A number can be added, averaged and compared. Text is a string of characters. If a cell holds the characters 1250 as text, it looks the same as the number 1250, but Excel treats it differently.
This difference produces errors and wrong results in several ways:
=A2+B2returns #VALUE! when a cell holds text that Excel cannot read as a number, such asn/aor12 units.=SUM(A2:A10)silently skips text values in the range. The total is too low and no error appears.=VLOOKUP(E2,A2:B100,2,FALSE)returns #N/A when the lookup value is the number 1001 and the table holds the text1001, or the other way round.- Sorting and filtering put text-numbers in the wrong order or in separate groups.
The second case is the most dangerous. A wrong total with no error message is easy to miss. That is why checking your data types is part of fixing errors, not only part of cleaning data.
Recognizing numbers stored as text
Several signs point to text-numbers. You rarely see all of them at once.
- Left alignment. By default, numbers align to the right of a cell and text aligns to the left. A column of numbers that sits on the left edge is suspect. This sign fails if someone set the alignment by hand.
- A green triangle. Excel marks the top-left corner of the cell. Hovering over the warning icon shows the message "Number Stored as Text".
- An apostrophe in the formula bar. If you type
'123into a cell, Excel keeps the value as text. The apostrophe does not show in the cell, but it shows in the formula bar. - The Number Format is Text. If a cell is formatted as Text before you type a number, Excel keeps what you type as text.
- The status bar. Select a range and look at the status bar at the bottom of the window. Excel shows Count, Sum and Average. If Sum is missing or lower than you expect, some cells are text.
You can also test a cell with a formula:
=ISNUMBER(A2)This returns TRUE for a real number and FALSE for text. =ISTEXT(A2) gives the opposite answer. Fill either formula down a helper column to see which rows are the problem.
=SUMPRODUCT(--ISTEXT(A2:A100))This counts the text cells in a range. If it returns anything above zero in a column that should hold only numbers, you have a cleaning job to do.
Hidden spaces and other invisible characters
A space is a character, and Excel counts it. The cell " 42" with a leading space is text. So is "42 " with a trailing space. You cannot see the difference by looking at the cell.
Spaces enter your data from several places:
- Exports from databases and accounting systems that pad fields to a fixed width.
- Text copied from web pages or PDFs.
- Manual typing, especially a trailing space at the end of a name or code.
You can measure the problem with LEN, which returns the number of characters in a cell:
=LEN(A2)If A2 shows 42 and LEN returns 3, there is one extra character. Comparing LEN(A2) with LEN(TRIM(A2)) tells you whether spaces are the cause.
The non-breaking space
Web pages often use a non-breaking space, which is character code 160. It looks like a normal space, but TRIM does not remove it. TRIM removes only the standard space, code 32.
If TRIM appears to do nothing, test for code 160:
=SUBSTITUTE(A2,CHAR(160)," ")This replaces each non-breaking space with a normal space. You can then wrap it in TRIM. CLEAN is a related function that removes non-printing characters with codes 0 to 31, such as line breaks from imported text. It does not remove code 160 either.
Converting text to numbers
The right method depends on how much data you have, and whether you want a formula or a one-time fix.
Using TRIM and VALUE in a formula
This formula removes leading and trailing spaces, and it also reduces runs of spaces between words to a single space. VALUE then converts the result to a number:
=VALUE(TRIM(A2))If the cell holds text that cannot be read as a number, such as n/a, VALUE returns #VALUE!. That is useful information. It tells you which cells need a manual decision instead of a conversion.
For data with non-breaking spaces, combine the three functions:
=VALUE(TRIM(SUBSTITUTE(A2,CHAR(160)," ")))Put the formula in a helper column, fill it down, then copy the results and use Paste Special > Values over the original column. Delete the helper column afterward.
Using the green triangle
Select the affected cells. Click the warning icon that appears next to them, then choose Convert to Number. This is quick for small ranges. It handles cells flagged as "Number Stored as Text", but it does not remove non-breaking spaces.
Using Text to Columns
Select the column, go to Data > Text to Columns, and choose Finish without changing any settings. Excel re-reads each cell and converts values that look like numbers. This works on a whole column at once and needs no formula. It handles one column at a time.
Using Paste Special
Type 1 in an empty cell and copy it. Select the text-numbers, choose Paste Special, then Multiply. Excel multiplies each cell by 1, which forces a conversion to a number. Text that is not numeric produces #VALUE! in the cell.
Comparing the methods
| Method | Handles non-breaking spaces | Needs a helper column | Best for |
|---|---|---|---|
| VALUE(TRIM(...)) | Only with SUBSTITUTE added | Yes | Repeatable cleaning and mixed data |
| Convert to Number (green triangle) | No | No | Small ranges flagged by Excel |
| Text to Columns | Sometimes | No | One column of imported data |
| Paste Special > Multiply | No | No | Quick conversion of an already clean range |
Fixing #VALUE! from the plus operator
The operators +, -, * and / try to read text as a number. If it works, the formula succeeds. If not, the result is #VALUE!. A cell with a regular space inside it, such as " ", is not an empty cell, and =A2+B2 returns #VALUE! when B2 contains only a space.
Functions such as SUM behave differently. They ignore text found in cell references. This formula returns a number even when B2 holds n/a:
=SUM(A2,B2)That is convenient, but remember the earlier warning. If B2 holds the text "250" and you expected it to count, SUM skips it. Use SUM when text should be ignored. Use + when a text value should raise an error so that you notice it.
A worked example
You import a sales export. Column A holds order IDs, and column B holds amounts. The total in B12 is =SUM(B2:B11), and it shows 1,800 when the amounts should add to 2,300.
- Select B2:B11 and read the status bar. Count shows 10, but Sum shows 1,800 and no value is flagged. This tells you some cells are text.
- In C2, enter
=ISNUMBER(B2)and fill it down. Two rows return FALSE. - In D2, enter
=LEN(B2). The two failing rows show a length one character longer than the visible digits. They contain a trailing non-breaking space. - In E2, enter
=VALUE(TRIM(SUBSTITUTE(B2,CHAR(160)," ")))and fill it down. - Check that E12,
=SUM(E2:E11), now returns 2,300. - Copy E2:E11, paste as values over B2:B11, and delete the helper columns C to E.
The method is the same for most imports: find which cells fail, find out why, convert, and verify the total.
Edge cases
- Leading zeros. A code such as
00123becomes 123 after conversion to a number. If the zeros matter, as in postal codes or product codes, keep the column as text and make your lookup values text too. - Decimal and thousands separators. Text such as
1.250,50follows European conventions. In a locale that expects1,250.50,VALUEmay return #VALUE! or a wrong number. UseSUBSTITUTEto swap the separators before converting. - Dates stored as text.
VALUEalso converts date text to a serial number, but the result depends on the regional settings. Format the result as a date afterward. - Formatting does not convert. Changing a cell from Text to General does not convert existing content. You must re-enter the cell, for example by pressing F2 and then Enter, or use one of the methods above.
- Lookups. To make a lookup tolerant of mixed types, convert the lookup value:
=VLOOKUP(VALUE(E2),A2:B100,2,FALSE). Fixing the source column is better, because other formulas also depend on it.
Summary of the key points
- A value that looks like a number can be text. Check with ISNUMBER, the status bar, alignment and the green triangle.
- Hidden spaces make a number into text. Use LEN to detect them, TRIM to remove normal spaces, and SUBSTITUTE with CHAR(160) for non-breaking spaces.
- Operators such as
+return #VALUE! for unreadable text. Functions such as SUM skip text silently. - Fix the data, not only the formula, so that every formula using the column works.
Exercise
Cell A2 shows 75 but =ISNUMBER(A2) returns FALSE, and =LEN(A2) returns 3. =VALUE(TRIM(A2)) still returns #VALUE!. What is the likely cause, and what formula fixes it?
Show answer
The cell most likely contains a non-breaking space (character code 160) alongside the digits 75. TRIM removes only the standard space (code 32), so it leaves the character in place and VALUE fails. Replace the non-breaking space first: =VALUE(TRIM(SUBSTITUTE(A2,CHAR(160)," "))).
Exercise
A column of amounts totals 4,100 with =SUM(B2:B20), but the status bar shows a count of 19 and you expect a total of 4,600. No error appears anywhere. How do you find the problem cells, and why did SUM not return an error?
Show answer
Some cells in B2:B20 are text-numbers. SUM skips text in cell references, so it returns a lower total without an error. Fill =ISNUMBER(B2) down a helper column, or use =SUMPRODUCT(--ISTEXT(B2:B20)) to count the text cells. Then convert them with Text to Columns, Convert to Number, or =VALUE(TRIM(B2)).
Exercise
You need to keep the product code 00482 exactly as written, and a VLOOKUP using it fails with #N/A. The table has the code as text but E2 holds the number 482. What should you change?
Show answer
Keep the codes as text, because converting would drop the leading zeros. Make the lookup value text with the same leading zeros, for example =VLOOKUP(TEXT(E2,"00000"),A2:B100,2,FALSE), so both sides are text and match.