Sub topic 4
Numbers stored as text and hidden spaces
This chapter assumes you can enter formulas, use cell references such as A2, and tell numbers from text in a cell. It builds on the #VALUE! chapter. Here you will find the data problem behind many of those errors and fix it.
Why a number can be text
Excel stores each cell value as a number, text, a date, or a logical value. A cell that looks like 125 may hold the text characters "1", "2", "5". Excel treats that cell as text, and arithmetic on it can fail or give wrong results.
Common sources of text numbers:
- Data pasted from a web page, a PDF, or an email.
- CSV or system exports that wrap values in quotes.
- A cell formatted as Text before the value was typed.
- A leading apostrophe, as in
'125. - Spaces before or after the value, including non-breaking spaces from web pages.
How to spot the problem
Check these signs before you change any formula:
- The value is left-aligned. Numbers align right by default.
- A small green triangle appears in the corner of the cell, with the warning "Number Stored as Text".
SUMreturns 0 or a total that is too low, because it skips text in ranges.=A2+B2returns #VALUE! when a cell holds text that is not a valid number.
Use =ISNUMBER(A2) to test a cell. It returns TRUE for a real number and FALSE for text.
Fix 1: TRIM and VALUE in a formula
TRIM removes leading and trailing spaces and reduces repeated spaces inside the text to one. VALUE converts text that looks like a number into a real number. Combine them:
=VALUE(TRIM(A2))If A2 contains " 125 " with spaces, the result is the number 125. Fill the formula down, then copy the results and paste them as values over the original data.
TRIM does not remove non-breaking spaces, which have character code 160. If VALUE(TRIM(A2)) still fails, replace them first:
=VALUE(TRIM(SUBSTITUTE(A2,CHAR(160)," ")))Fix 2: Convert in place
When the data has no hidden spaces, you can convert it without a helper column:
- Select the affected cells.
- Click the yellow warning icon that appears next to the selection.
- Choose Convert to Number.
Another option is Data > Text to Columns. Select the column, choose Finish on the first step, and Excel re-reads each value, converting numbers stored as text.
Fix 3: Avoid the problem in the formula
Some functions ignore text in cell references. Compare these two formulas when A2 holds a number and B2 holds the text "n/a":
=SUM(A2,B2)SUM skips the text and returns the value of A2. The formula =A2+B2 returns #VALUE! for the same cells. Be careful with this fix. A number stored as text is also skipped, so the total quietly becomes too low. Use SUM to tolerate genuine text such as "n/a", and clean the data when the values should be numbers.
Worked example
Column A holds quantities imported from a system export. =SUM(A2:A5) shows 0.
| Cell | Content | ISNUMBER |
|---|---|---|
| A2 | `"10"` (text) | FALSE |
| A3 | `" 20"` (leading space) | FALSE |
| A4 | `30` (number) | TRUE |
| A5 | `"40 "` (trailing space) | FALSE |
- In B2, enter
=VALUE(TRIM(A2))and fill down to B5. - B2:B5 now show 10, 20, 30 and 40, all right-aligned.
=SUM(B2:B5)returns 100.
A4 was already a number, so SUM found it. With only A4 counted, the total would have been 30, not 0. This is why a total that is too low is as much a warning as an error value.
Common mistakes
- Changing the cell format to Number and expecting existing text to convert. Excel converts the value only after you re-enter it, for example by pressing F2 then Enter.
- Wrapping formulas in
IFERRORbefore cleaning the data. This hides the cause, a point the later chapter on preventing versus masking errors develops. - Leaving the helper column as formulas and then deleting the original column, which causes #REF!.
Exercise
A2 contains the text " 75" with two leading spaces, and =A2*2 returns #VALUE!. Write a formula that returns 150, and explain why it works.
Show answer
Use =VALUE(TRIM(A2))*2. TRIM removes the leading spaces, VALUE turns the remaining text "75" into the number 75, and multiplying by 2 gives 150. If the spaces are non-breaking (CHAR(160)), use =VALUE(TRIM(SUBSTITUTE(A2,CHAR(160)," ")))*2.
Exercise
A column of 200 imported prices is summed with =SUM(B2:B201) and gives 0, with no error. Why is there no error, and what is the quickest check?
Show answer
SUM ignores text in cell references, so text numbers are skipped instead of causing an error. Test a cell with =ISNUMBER(B2). FALSE confirms the values are text, and Convert to Number or Text to Columns fixes them.