Sub topic 10
Detecting and handling blank cells
This chapter assumes you can write a basic IF formula with a logical test, a value if true, and a value if false. It also assumes you can use comparison operators and copy a formula down a column with the fill handle. Here you will learn how to make IF respond to empty cells so that unfinished rows stay quiet and do not show misleading results.
Why blank cells need special treatment
A blank cell is not an error, but Excel does not treat it as "nothing" either. In arithmetic, an empty cell counts as 0. In a numeric comparison, it also behaves like 0. Suppose B2 is empty and you write this formula:
=IF(B2>=60,"Pass","Fail")Excel reads the blank as 0, so it returns "Fail". The student may simply not have taken the test yet, so "Fail" is a wrong answer. The formula has no way to know the difference unless you ask.
The fix is to test for blank first. If the input is missing, return an empty result. Otherwise, run the real decision.
=IF(B2="","",IF(B2>=60,"Pass","Fail"))The outer IF asks "is B2 empty?". If so, the formula returns "", which is an empty text string and displays as nothing. If not, the inner IF runs your normal logic.
Three ways to test for blank
Excel gives you several tests that look interchangeable. They differ in edge cases, and those differences cause most blank-cell bugs.
=IF(A2="","Missing","Present")
=IF(ISBLANK(A2),"Missing","Present")
=IF(LEN(A2)=0,"Missing","Present")A2=""compares the cell with an empty string. It is the most common form and is easy to read.ISBLANK(A2)returns TRUE only when the cell is truly empty, with nothing in it at all.LEN(A2)=0is TRUE when the cell has zero characters in it.
The differences show up when a cell is not truly empty but only looks that way. The table shows how each test responds to four cell states. The "formula returning empty text" row means a cell that contains something like =IF(X1>5,"","").
| What the cell contains | `A2=""` | `ISBLANK(A2)` | `LEN(A2)=0` | `TRIM(A2)=""` |
|---|---|---|---|---|
| Truly empty | TRUE | TRUE | TRUE | TRUE |
| Formula returning empty text | TRUE | FALSE | TRUE | TRUE |
| A single space | FALSE | FALSE | FALSE | TRUE |
| The number 0 | FALSE | FALSE | FALSE | FALSE |
Two points follow from the table.
First, ISBLANK is the strictest test. If A2 holds a formula that returns "", ISBLANK(A2) is FALSE, because the cell contains a formula. That matters when one column feeds another, as the later section on chained formulas shows.
Second, the number 0 is not blank. A2="" is FALSE for a cell that holds 0, even though an empty cell compared with 0 (A2=0) is TRUE. If 0 is a valid entry in your data, such as a score of zero, always use the ="" test. Do not rely on A2=0 to mean "nothing entered".
The standard guard pattern
The most useful blank-cell pattern checks an input, then either stays empty or does the work.
=IF(A2="","",B2*C2)This leaves the result empty when A2 is blank, and otherwise multiplies B2 by C2. Copy it down a column of an order sheet and rows you have not filled in yet stay clean. Without the guard, those rows would show 0, which looks like a real result.
You can also flip the test with the <> operator, which means "not equal to". These two formulas give identical results:
=IF(A2="","",B2*C2)
=IF(A2<>"",B2*C2,"")Choose whichever reads more naturally to you. Putting the blank case first keeps the "do nothing" branch short and visible.
Several inputs: AND and OR
Often a row needs more than one input before it is ready. Combine blank tests with AND and OR, which you met in the previous chapter.
To calculate only when all inputs are present, use AND with <>"":
=IF(AND(A2<>"",B2<>"",C2<>""),B2*C2,"")To stay empty when any input is missing, use OR with ="":
=IF(OR(A2="",B2="",C2=""),"",B2*C2)These two formulas do the same thing. They are mirror images, built on the same logic as the rule that "not (this or that)" equals "not this and not that". Pick the one you can read aloud most easily.
For a long run of cells, counting is shorter than listing every cell. COUNTA counts non-empty cells and COUNTBLANK counts empty ones:
=IF(COUNTA(A2:C2)=3,SUM(A2:C2),"")This returns a total only when all three cells in the row are filled. The 3 is the number of cells in the range, so adjust it if you change the range.
Cells that look blank but are not
Data pasted from a web page, a database export, or another person's workbook often has cells that look empty and fail the ="" test. Two causes are common:
- A cell holds a space, or several, typed or exported by accident.
- A cell holds a non-breaking space, character code 160, which is common in web data.
To diagnose, put =LEN(A2) in a spare cell. A "blank" cell that returns 1 or more has hidden content. TRIM removes ordinary leading, trailing, and repeated spaces, so you can include it in the test:
=IF(TRIM(A2)="","Missing","Present")TRIM does not remove the non-breaking space. To handle it, replace character 160 with a normal space first:
=IF(TRIM(SUBSTITUTE(A2,CHAR(160)," "))="","Missing","Present")This is longer, so use it only when LEN shows the simpler test is failing. Cleaning the source data once is often better than complicating every formula that reads it.
Chained formulas: when empty text causes errors
A formula that returns "" produces text, not a number. That is fine for display but can break a formula that uses the result later.
Suppose D2 holds =IF(A2="","",B2*C2) and is empty for an unfinished row. A second formula then adds to it:
=D2+10This returns #VALUE! when D2 contains "", because Excel cannot add a number to text. You have three common remedies.
Guard the second formula in the same way:
=IF(D2="","",D2+10)Or use N, which converts text to 0 and leaves numbers unchanged:
=N(D2)+10Or use a function that ignores text in cell references, such as SUM:
=SUM(D2,10)Choose by the result you want. The first leaves the second column empty too. The other two treat the missing value as 0 and return 10.
Summary functions behave differently depending on whether you return "" or 0 for unfinished rows. SUM, AVERAGE, MIN, and MAX ignore text, so cells holding "" are skipped. A 0 is a number and is included. Averaging a column of scores where unfinished rows show 0 will pull the average down. Returning "" keeps unfinished rows out of the average.
Blank thresholds
Blank handling also matters when the cell you compare against is empty. Recall the threshold pattern from earlier in this tutorial:
=IF(B2>=$F$1,"Target met","Below target")If F1 is empty, Excel treats it as 0, so every non-negative value reports "Target met". That is a silent failure, because the sheet looks normal while every answer is wrong. Guard against it by checking the threshold first:
=IF($F$1="","Set target in F1",IF(B2>=$F$1,"Target met","Below target"))Now a missing threshold produces a visible message in every row. Write guards like this when the threshold is entered by a person who might forget.
Common mistakes with blank tests
| Symptom | Likely cause | Fix |
|---|---|---|
| Unfinished rows show "Fail" or 0 | No blank test, so the empty cell counts as 0 | Add `IF(A2="","",...)` as the outer test |
| `""` test is FALSE on a cell that looks empty | The cell holds a space or character 160 | Check `LEN(A2)`, then use `TRIM` or `SUBSTITUTE` |
| `ISBLANK` is FALSE on a cell that shows nothing | The cell holds a formula returning `""` | Use `A2=""` instead |
| `#VALUE!` in a column that reads a guarded column | Arithmetic on `""` text | Guard again, or use `N` or `SUM` |
| Valid zero treated as missing | The test used `A2=0` or `NOT(A2)` | Test with `A2=""` |
| Average is lower than expected | Unfinished rows return 0 | Return `""` instead of 0 |
A final habit: test every blank guard on three kinds of rows. Use a row that is fully filled, a row that is completely empty, and a row that is half filled. Many guards pass the first two and fail the third.
Exercises
Exercise
A2 contains a formula that returns an empty string. What do =ISBLANK(A2) and =A2="" return?
Show answer
ISBLANK(A2) returns FALSE, because the cell contains a formula and is not truly empty. A2="" returns TRUE, because the formula's result is an empty string.
Exercise
Write a formula for D2 that returns the average of B2 and C2, but stays empty unless both B2 and C2 contain something.
Show answer
=IF(OR(B2="",C2=""),"",AVERAGE(B2:C2)). An equivalent form is =IF(AND(B2<>"",C2<>""),AVERAGE(B2:C2),"").
Exercise
Column E holds =IF(A2="","",B2*C2). Cell F2 holds =E2*2. For a row where A2 is empty, F2 shows #VALUE!. Give two ways to fix F2 and say how the results differ.
Show answer
Guard it with =IF(E2="","",E22), which leaves F2 empty. Or use =N(E2)2, which treats the missing value as 0 and returns 0. The first keeps unfinished rows visibly empty. The second shows 0 for them.