Writing Spreadsheet Formulas
Write formulas that use cell references, SUM, AVERAGE and IF, lock a reference with dollar signs, and find and fix the error values that break a sheet.
Table of contents
A formula tells the spreadsheet to work something out for you: a line total, a monthly sum, an average, a flag that says "Reorder". Done right, the answer updates itself when the numbers change. Done wrong, it gives a confident wrong answer that nobody notices for months. Formulas, functions and error values are the same in Excel and Google Sheets, with a few differences noted below.
What You Will Be Able to Do
- Write a formula that points at cells instead of typed-in numbers.
- Predict how a formula changes when you copy it, and lock a reference when it must not change.
- Use SUM, AVERAGE, MIN, MAX, COUNT, COUNTA, ROUND, COUNTIF, SUMIF and IF.
- Read an error value such as
#DIV/0!or#N/Aand fix its cause. - Check a formula before you trust its answer.
How a Formula Works
Every formula starts with an equals sign. Type = in a cell, then the math, then press Enter. The cell shows the result, and the formula bar shows the formula.
| Symbol | Meaning | Example | Result |
|---|---|---|---|
+ - |
Add, subtract | =10-4 |
6 |
* / |
Multiply, divide | =6*3 |
18 |
^ |
Raise to a power | =2^3 |
8 |
& |
Join text | ="Unit "&"4B" |
Unit 4B |
Comparisons give TRUE or FALSE: = equal, <> not equal, > greater than, < less than, >= and <=.
The program does math in a set order: parentheses first, then powers, then multiply and divide, then add and subtract. =2+3*4 returns 14, because 3 x 4 happens first. =(2+3)*4 returns 20. When in doubt, add parentheses.
Point at cells, do not type numbers. If B2 holds a quantity and C2 holds a price, write =B2*C2, not =4*6.5. When the price changes, the first formula updates and the second stays wrong. You can type the address, or click the cell while you are writing the formula.
As you type a function name, both programs show a list of matches and then a tip listing the arguments, which are the values the function needs. In the United States, arguments are separated by commas. In some countries' settings, the separator is a semicolon.
Worked Example: An Order Sheet
| A | B | C | |
|---|---|---|---|
| 1 | Item | Qty | Unit Price |
| 2 | Gloves (box) | 4 | 6.50 |
| 3 | Tape (roll) | 12 | 2.25 |
| 4 | Markers (pack) | 3 | 8.99 |
The sales tax rate, 8.25% in this example, sits in cell F1 with the label "Tax rate" in E1. Column D holds the line totals.
| Cell | Formula | Result |
|---|---|---|
| D2 | =B2*C2 |
26.00 |
| D3 | =B3*C3 |
27.00 |
| D4 | =B4*C4 |
26.97 |
| D5 | =SUM(D2:D4) |
79.97 |
| D6 | =ROUND(D5*$F$1,2) |
6.60 |
| D7 | =D5+D6 |
86.57 |
You type only D2. Then drag its fill handle down to D4, and the program writes D3 and D4 for you. Without ROUND, D6 would hold 6.597525 and show 6.60, and the total would carry the extra decimals.
Relative and Absolute References
When you copy a formula, its references move with it. =B2*C2 copied down one row becomes =B3*C3. This is called a relative reference, and it is what you want most of the time.
Sometimes a reference must stay put, such as the tax rate in F1. Suppose you write =D2*F1 in E2 and copy it down. E3 becomes =D3*F2, and F2 is empty, so the tax comes out 0. Dollar signs fix this. $F$1 is an absolute reference, and it stays F1 wherever you copy it.
| Reference | Copied down a row | Copied right a column |
|---|---|---|
A1 |
Becomes A2 | Becomes B1 |
$A$1 |
Stays A1 | Stays A1 |
A$1 |
Stays A1 | Becomes B1 |
$A1 |
Becomes A2 | Stays A1 |
The last two are mixed references. The dollar sign locks only the part right after it.
To add the dollar signs, click on the reference while you are editing and press F4. Each press moves to the next combination. F4 works in Excel for Windows and in Google Sheets. Excel for Mac uses Command+T. You can also type the dollar signs yourself.
Mixed reference example. Base prices are in A2 and A3. Markups of 10% and 25% are in B1 and C1. In B2, write =$A2*(1+B$1) and copy it to B2:C3.
| A | B | C | |
|---|---|---|---|
| 1 | Base | 10% | 25% |
| 2 | 40.00 | 44.00 | 50.00 |
| 3 | 12.00 | 13.20 | 15.00 |
$A2 keeps pointing at column A as the formula moves right, and B$1 keeps pointing at row 1 as it moves down. One formula fills the whole grid.
References to another sheet put the sheet name and an exclamation point in front: =Prices!B2. If the sheet name has a space, wrap it in single quotes: ='Price List'!B2. Clicking the other sheet's tab while you write the formula adds this for you.
Functions You Will Use Every Day
A function is a named formula that takes arguments in parentheses. All of these work the same way in Excel and Google Sheets.
| Function | What it does | Example |
|---|---|---|
| SUM | Adds the numbers | =SUM(D2:D50) |
| AVERAGE | Average of the numbers | =AVERAGE(B2:B50) |
| MIN, MAX | Smallest, largest | =MAX(C2:C50) |
| COUNT | Counts cells with numbers | =COUNT(B2:B50) |
| COUNTA | Counts cells that are not empty | =COUNTA(A2:A50) |
| ROUND | Rounds to set decimals | =ROUND(D6,2) |
| COUNTIF | Counts cells that meet a test | =COUNTIF(C2:C50,"Open") |
| SUMIF | Adds the cells that meet a test | =SUMIF(A2:A50,"Tape",D2:D50) |
In Excel, AutoSum (Home > AutoSum, or Alt+=) writes a SUM for the cells above. In Google Sheets, the Functions button on the toolbar inserts SUM and other common functions.
Blank cells are not zeros. AVERAGE, COUNT, MIN and MAX skip empty cells and text, but a 0 counts. Four meter readings of 10, blank, 0 and 20 give =AVERAGE(A2:A5) = 10, because (10 + 0 + 20) / 3 = 10. If the blank had been a 0, the average would be 7.5. Decide whether a missing reading means "zero" or "not taken" before you leave a cell empty.
COUNT and COUNTA count different things. In a column holding 12, "n/a", 7 and one blank cell, COUNT returns 2 and COUNTA returns 3.
COUNTIF and SUMIF tests go in double quotes when they hold text or a comparison sign: "Open", ">100", "<>Closed". A test against another cell joins the sign with &, as in ">="&F1. Text tests ignore capital letters, so "open" and "Open" both match.
Making Decisions with IF
IF checks a test and returns one answer when it is true and another when it is false. The arguments are the same in both programs: the test, the value if true, then the value if false.
| A | B | C | |
|---|---|---|---|
| 1 | Item | On Hand | Reorder At |
| 2 | Air filters | 6 | 10 |
| 3 | Gloves | 24 | 12 |
| 4 | Tape | 12 | 12 |
In D2, write =IF(B2<=C2,"Reorder","OK") and copy it down. D2 shows Reorder, D3 shows OK, and D4 shows Reorder, because 12 is less than or equal to 12. Text answers go in double quotes. Numbers and cell references do not.
Useful patterns:
- Leave a row blank until it has data:
=IF(B2="","",B2*C2). Two double quotes with nothing between them mean "empty". - Two conditions at once:
=IF(AND(B2>0,C2="Yes"),"Ship","Hold"). Use OR when either condition is enough. - More than two answers: put one IF inside another.
=IF(B2>=1000,"Large",IF(B2>=250,"Medium","Small"))checks the biggest cutoff first. Excel 2019 and later, Microsoft 365 and Google Sheets also have IFS, which lists the tests in order without nesting.
Error Values and What They Mean
An error value is the program telling you something is wrong. Read it instead of hiding it.
| Error | Usual cause | Fix |
|---|---|---|
#DIV/0! |
Dividing by zero or by an empty cell | Fill the cell, or test for 0 with IF |
#N/A |
A lookup could not find the value | Check spelling, spaces and numbers stored as text |
#NAME? |
A misspelled function, text missing its quotes, or a function your version lacks | Fix the spelling or add the quotes |
#REF! |
The formula points at cells that were deleted | Undo the delete, or rebuild the reference |
#VALUE! |
Math on text, such as "12 boxes" x 2 | Make the cell a plain number |
#NUM! |
An impossible number, such as a square root of a negative | Check the inputs |
#SPILL! |
Excel only. A result needs empty cells that are not empty | Clear the cells in its way |
#ERROR! |
Google Sheets only. The formula cannot be read, often a missing quote or parenthesis | Retype the formula carefully |
Google Sheets also uses #REF! when a formula that returns several cells is blocked by data, and when a formula refers to its own cell. Point at the error, or click the cell, to see the message. A formula that refers to its own cell, directly or through others, is a circular reference. Excel shows a warning and lists it on the status bar.
In Excel, #### is not an error. The column is too narrow to show the number.
Use IFERROR with care. =IFERROR(A2/B2,0) shows 0 instead of #DIV/0!. That is fine when a blank divisor really means zero. It is a problem when the error was telling you about a typo, because now the typo looks like a real 0.
Check a Formula Before You Trust It
- Hand-check one row. Work one line out with a calculator. If D2 should be 4 x 6.50 = 26.00, make sure it is.
- Check the range. Click into the formula. Both programs outline each referenced range in color. Make sure
=SUM(D2:D20)covers every row, including the last one. - Show every formula. Press Ctrl+` (the key left of 1) in either program to switch the sheet between results and formulas. In Excel, it is also Formulas > Show Formulas. In Google Sheets, it is View > Show > Formulas. A number where you expect a formula means someone typed over it.
- Trace it (Excel). Formulas > Trace Precedents draws arrows to the cells a formula uses. Evaluate Formula steps through it one piece at a time. Google Sheets has no arrows, so rely on the colored outlines.
- Compare with the status bar. Select the cells and read the sum at the bottom of the window. It should match your SUM formula.
Common Mistakes
| Mistake | What it costs |
|---|---|
Typing a number into a formula, like =D5*0.0825 |
When the rate changes, nobody finds every formula that needs it |
| Forgetting the dollar signs on a fixed cell | Every row below the first uses the wrong cell |
| A SUM that stops short of new rows | Rows added below the range are left out of the total |
| Typing over a formula with a number | The cell stops updating, and it looks just like the others |
| Hiding every error with IFERROR | Real mistakes turn into believable zeros |
| Leaving numbers stored as text | SUM skips them and gives a total that is too low |
To keep a total growing with the list, insert new rows above the last row of the range, so the range stretches. Or turn the list into a table (Insert > Table in Excel, Format > Convert to table in Google Sheets). A total that refers to a table column by its name keeps up as rows are added.
Check Your Understanding
1. You write =C2*G1 in D2 to apply a discount rate stored in G1, then copy it down. D3 and below show 0. Why, and what is the fix?
The reference to G1 is relative, so D3 points at G2, which is empty. Change it to =C2*$G$1 and copy it down again.
2. A column of call times holds 4, 6, blank and 8. Your supervisor says the average is 6. A coworker says it is 4.5. Who is right?
Your supervisor, if the blank means "no call". AVERAGE skips the blank: (4 + 6 + 8) / 3 = 6. The coworker counted the blank as a zero: 18 / 4 = 4.5. Ask what the blank means before you report either number.
3. A cell shows #NAME? after you type =SUM(B2:B10)+Tax. What went wrong?
The program read Tax as a name it does not know. Point at the cell that holds the tax, such as =SUM(B2:B10)+B11, or put text in double quotes if you meant text.
4. Your IF formula is =IF(B2>50,"High","Low"). B2 holds 50. What does it return, and what would you change to call 50 "High"?
It returns Low, because 50 is not greater than 50. Use >= to include 50: =IF(B2>=50,"High","Low").
Key Takeaways
- Start with
=, point at cells, and keep rates and other inputs in their own labeled cells. - References move when you copy a formula. Lock a fixed cell with dollar signs, using F4.
- Blank cells are skipped by AVERAGE and COUNT, but zeros count.
- Put text in double quotes inside formulas.
- An error value names the problem. Fix the cause before you reach for IFERROR.
- Hand-check one row and show formulas before you trust a sheet.