Sub topic 5
The order Excel calculates in
This chapter explains how Excel decides which part of a formula to calculate first. It assumes you can open a workbook, type a value into a cell, press Enter, and write a simple formula that starts with =. It also assumes you know the idea of order of operations from basic arithmetic.
Why order matters
A formula with more than one operator can give different results depending on which operation runs first. Excel does not read left to right and apply each operator as it meets it. It follows fixed precedence rules, which are the same rules you learned in arithmetic class, with a few Excel-specific details.
Take this formula:
=2+3*4If Excel went strictly left to right, it would compute 2+3 = 5, then 54 = 20. It does not. Multiplication ranks higher than addition, so Excel computes 34 = 12 first, then adds 2. The result is 14.
The precedence table
Excel ranks the operators covered in this tutorial like this, from first to last:
| Order | Operator | Meaning |
|---|---|---|
| 1 | `-` (before a value) | Negation, as in `-5` |
| 2 | `%` | Percent |
| 3 | `^` | Exponent |
| 4 | `*` and `/` | Multiply and divide |
| 5 | `+` and `-` | Add and subtract |
Operators in the same row have equal rank. When a formula contains several of them, Excel works from left to right. For example, =10-4+2 computes 10-4 = 6, then 6+2 = 8. =20/4*2 computes 20/4 = 5, then 5*2 = 10.
Walking through a formula
Here is a longer formula, evaluated one step at a time:
=10+6/2*3-1- Division and multiplication come first, left to right. 6/2 = 3, so the formula becomes
10+3*3-1. - Next, 3*3 = 9, so it becomes
10+9-1. - Addition and subtraction come last, left to right. 10+9 = 19, then 19-1 = 18.
The result is 18.
The diagram shows the same sequence as a flow.
Seeing the order with cell references
Precedence applies the same way when a formula uses cell references. Suppose A1 holds 100, B1 holds 20, and C1 holds 5.
=A1-B1/C1Excel divides first: B1/C1 = 20/5 = 4. Then it subtracts: 100-4 = 96. A common mistake is to expect (100-20)/5 = 16. If you want that result, you must say so with parentheses, which the next chapter covers.
Percent and negation
Percent ranks high. Excel treats 10% as the number 0.1 before any other operator acts on it.
=10%*200This returns 20, because 0.1 * 200 = 20.
Negation is the most common surprise. In Excel, the minus sign in front of a number ranks above ^. So this formula:
=-2^2first turns 2 into -2, then squares it. The result is 4. Many calculators and programming languages return -4 here, because they apply the exponent first. In Excel, you must write the grouping yourself to get -4:
=-(2^2)Common errors in applying precedence
- Assuming left to right for everything.
=2+3*4is 14, not 20. Only operators of equal rank run left to right. - Forgetting that division and multiplication tie. In
=8/2*4, Excel computes 8/2 = 4 first, then 44 = 16. It does not compute 24 first. The result is 16, not 1. - Averaging without grouping.
=A1+B1/2divides only B1 by 2, then adds A1. It does not average the two cells. - Trusting the display. Excel shows only the final result in the cell. If a number looks wrong, work through the formula by hand with the table above.
Checking your prediction
A good habit is to predict the result before you press Enter. Write the formula, apply the table, and compare your answer with what Excel shows. If they differ, one of two things is true: you misread the precedence, or the formula is not what you meant to write. Both are worth finding out early, before the formula is copied down a column of data.
Exercise
Without using Excel, what do the formulas =4+6/2^2 and =-3^2 return? Explain the order of steps for each.
Show answer
=4+6/2^2 returns 5.5. First 2^2 = 4, then 6/4 = 1.5, then 4+1.5 = 5.5. =-3^2 returns 9, because Excel applies the negation to 3 first, giving -3, and then squares it. Writing =-(3^2) would return -9.
Exercise
Cells A1, B1, and C1 hold 100, 20, and 5. What does =A1-B1/C1 return, and how would you change it to subtract B1 from A1 first and then divide by C1?
Show answer
It returns 96, because B1/C1 = 4 runs before the subtraction. To subtract first, use parentheses: =(A1-B1)/C1, which returns 16.