Sub topic 4
Returning text, numbers, and calculations
This chapter assumes you can enter formulas, use cell references such as B2, copy a formula down a column with the fill handle, and write a basic =IF(test, value_if_true, value_if_false) formula. Here the focus is the second and third arguments: what an IF formula can return, and what happens to the result afterward.
What the result arguments can hold
The logical test decides which branch runs. Each branch is an ordinary Excel expression. It can be any of these:
| Kind of result | Example argument | Notes |
|---|---|---|
| Text | `"Pass"` | Must be in double quotes. |
| Number | `100` | No quotes. The result is a real number. |
| Calculation | `B2*0.1` | Evaluated only when that branch is chosen. |
| Cell reference | `C2` | Returns the value stored in that cell. |
| Empty string | `""` | Looks blank but is text of length zero. |
| Another function | `ROUND(B2*0.1,2)` | Any function that returns a value. |
The two branches do not have to return the same kind of value. One branch can return text and the other a number. That is allowed, but it often causes trouble later, as the section on mixed types shows.
Returning text
Text results are the most common use. Put each text value in double quotes.
=IF(B2>=60,"Pass","Fail")If B2 holds 72, the cell shows Pass. If B2 holds 45, it shows Fail. Without the quotes, Excel would read Pass as a name it does not recognize and return #NAME?.
Text results are labels, so they work well for status columns, flags, and categories. They are poor inputs for further arithmetic. A cell showing Pass cannot be summed. If you need to count passes later, use COUNTIF on the column rather than trying to add the text.
Returning numbers
A number result is typed without quotes.
=IF(B2>=60,1,0)This returns the number 1 or 0. Unlike the text version, the output can be summed, averaged, or multiplied. A column of 1 and 0 values is a common way to build a flag that other formulas can total.
The quotes change the data type, not only the appearance. Compare these two formulas:
=IF(B2>=60,"1","0")This version returns the text "1" or "0". The cell may look identical, but SUM ignores text values in a range, so a total over this column would give 0. If a number column behaves as if it were empty, check for quotes around the numbers in the IF formula.
Returning calculations
A branch can contain a calculation. Excel computes the expression only for the branch that the test selects.
=IF(B2>=1000,B2*0.1,0)When sales in B2 reach 1000, the cell returns 10% of B2. Otherwise it returns 0. If B2 is 1500, the result is 150. If B2 is 800, the result is 0.
The same pattern works with any arithmetic. Here are some variations:
=IF(B2>=1000,B2*0.1,B2*0.05)pays 10% above the threshold and 5% below it. Both branches calculate.=IF(C2="Member",B2*0.9,B2)applies a 10% discount to members and returns the original price otherwise. The false branch is a plain cell reference.=IF(B2>100,B2-100,0)returns only the amount above 100, or 0 when there is none.
A calculation can also combine several cells. In =IF(A2="","",B2*C2), the true branch returns an empty string and the false branch multiplies two cells. Later chapters use this form for blank handling.
Rounding results
Calculated branches often produce long decimals. Wrap the calculation in ROUND so the stored value is rounded, not only the display.
=IF(B2>=1000,ROUND(B2*0.075,2),0)Number formatting changes how a value looks. ROUND changes the value. If the results will be added up, the difference matters: a column of values displayed to two decimals can sum to a total that differs by a cent from the sum of the displayed figures, unless the values were rounded first.
Returning a cell reference
A branch can point to another cell. This is useful when the answer depends on a value you keep elsewhere.
=IF(B2>=1000,$F$2,$F$3)Here F2 might hold a high bonus rate and F3 a low one. The formula returns whichever cell is chosen. The $ signs keep both references fixed when you copy the formula down. The next chapters explain absolute references in detail. For now, note that keeping values in cells rather than typing them into formulas makes the worksheet easier to update.
Returning an empty string
To make a result look empty, return two double quotes with nothing between them.
=IF(B2<60,"Review","")Students who score below 60 get Review. For everyone else, the cell appears blank.
An empty string is not an empty cell. The cell contains a formula, and the formula's result is text of length zero. This has three consequences:
ISBLANKreturns FALSE for the cell, because the cell is not truly empty.- A test such as
=E2=""returns TRUE, because that comparison matches both truly empty cells and empty strings. - Arithmetic that refers to the cell, such as
=E2+1, returns#VALUE!, because""cannot be treated as a number.
Functions that take a range, such as SUM and AVERAGE, skip text in the range. For that reason "" is safe in a column you only total with SUM, but unsafe when you reference individual cells with + or *.
Mixed result types
Excel allows one branch to be text and the other a number.
=IF(C2=0,"No data",B2/C2)If C2 is 0, the cell shows No data. Otherwise it shows a number. This is practical for display, but the column now holds a mix of text and numbers. Consider what happens downstream:
SUMandAVERAGEover the column skip the text, so the average covers only the numeric rows. That may or may not be what you want.=F2*2on a row that holdsNo datagives#VALUE!.- Sorting places numbers and text in separate groups.
- Alignment differs. Numbers align right by default and text aligns left, so a mixed column looks uneven.
Decide first how the results will be used. If a column feeds more calculations, keep it numeric and choose a number such as 0 for the fallback case. If a column is only for a reader, text labels are fine.
Results that look like numbers but are text
If an IF formula gives surprising results, check whether the value is text or a number. Three functions help:
ISNUMBER(G2)returns TRUE if G2 holds a number.ISTEXT(G2)returns TRUE if G2 holds text, including"".LEN(G2)returns the length of the text. For""it returns 0.
You can also spot text by alignment. Without custom formatting, a number is right-aligned and text is left-aligned.
Building a combined text result
A branch can build a sentence from pieces by joining text and values with the & operator.
=IF(B2>=60,"Passed with "&B2&" points","Failed with "&B2&" points")If B2 is 72, the result is Passed with 72 points. The & operator joins text and values into one text string. The result is text, even though it contains a number.
Joined numbers use General formatting, so a value such as 0.1 shows as 0.1 and a long decimal shows many digits. Use TEXT to control the format:
=IF(B2>=1000,"Bonus: "&TEXT(B2*0.1,"0.00"),"No bonus")If B2 is 1234.5, the result is Bonus: 123.45. The second argument of TEXT is a format code, written in quotes.
A worked example
A shop pays commission of 8% on sales of 2000 or more and 3% on lower sales. Staff who sell nothing get a note instead of a payment. The data is in columns A (name) and B (sales).
| A | B | C (commission) |
|---|---|---|
| Ana | 2500 | 200 |
| Ben | 1200 | 36 |
| Cy | 0 | No sales |
The formula in C2 is:
=IF(B2=0,"No sales",IF(B2>=2000,B2*0.08,B2*0.03))This uses a nested IF, covered in a later chapter. It shows that the result types can vary by row: text for Cy, calculated numbers for the others. Because C now mixes text and numbers, SUM(C2:C4) still works, since SUM skips the text. A formula such as =C4*2 would not.
A simpler two-outcome version avoids the mix:
=IF(B2>=2000,B2*0.08,B2*0.03)Here Cy would receive 0, which is a valid number. Whether you want the note or the zero depends on how the column is used.
Common mistakes with results
- Missing quotes around text.
=IF(B2>=60,Pass,Fail)returns#NAME?. Add quotes:"Pass","Fail". - Quotes around numbers.
"100"is text. Use100when you need a number. - Percent as text. To return 10%, write
0.1or10%. Do not write"10%". - Forgetting to multiply.
=IF(B2>=1000,0.1,0)returns the rate 0.1, not 10% of B2. WriteB2*0.1when you want the amount. - Leaving a branch empty. In
=IF(B2>=60,"Pass",), the omitted false value returns0. If you want a blank look, write""explicitly. - Text with a currency symbol. Returning
"$"&B2gives text. For a numeric result, returnB2and apply a currency number format.
Summary of choices
Pick the type of result by what happens next:
- Use text when the cell is a label for people to read.
- Use numbers (including
0) when other formulas will calculate with the cell. - Use
""when the cell should look empty and will only be read by people or totaled withSUM. - Use a calculation when the answer depends on the input values.
- Use
ROUNDwhen the calculated value is stored or summed.
Exercise
Column B holds order totals. Write a formula for C2 that returns a 5% discount amount (as a number) when B2 is 500 or more, and 0 otherwise. Then explain why =IF(B2>=500,"5%","0") would be a poor choice if you plan to total column C.
Show answer
Use =IF(B2>=500,B2*0.05,0). It returns a real number in both branches, so SUM can total column C. The version with "5%" and "0" returns text, because the values are in quotes. It also does not calculate the discount from B2. SUM skips text, so a total over that column would be 0.
Exercise
Cell E2 contains =IF(B2<60,"Review",""). Another cell contains =E2+1. What does the second cell show for a student with B2 equal to 80, and how would you change the first formula so the second cell works?
Show answer
It shows #VALUE!, because E2 holds an empty string, which is text and cannot be added to 1. Return a number instead, for example =IF(B2<60,1,0). If you want the zeros to look blank, apply a number format such as 0;; to the cells, or keep the "" formula and avoid + on that column.