Sub topic 13
Next step: SUMIF, AVERAGEIF, and COUNTIF
This chapter assumes you can write SUM, AVERAGE, and COUNT formulas over a range. It also assumes you know how relative and absolute references behave when a formula is copied, and that you've seen how these functions skip blanks and text. Here you add a condition, so a function reads only the cells that meet a test.
Why add a condition
SUM, AVERAGE, and COUNT read every numeric cell in the range you give them. Real questions are usually narrower. How much did the East region sell? What is the average of East orders only? How many orders were over 100?
You could sort the data or build a helper column. The conditional functions answer these questions in one formula, and the answer updates when the data changes.
| Plain function | Conditional version | Question it answers |
|---|---|---|
| SUM | SUMIF | What is the total of the cells that match? |
| AVERAGE | AVERAGEIF | What is the mean of the cells that match? |
| COUNT | COUNTIF | How many cells match? |
The examples in this chapter use this data. Region is in A2:A6 and Sales is in B2:B6.
| Row | A (Region) | B (Sales) |
|---|---|---|
| 2 | East | 120 |
| 3 | West | 80 |
| 4 | East | 150 |
| 5 | North | 95 |
| 6 | East | 60 |
Anatomy of the three functions
The three functions share a pattern. You name a range to test and a criterion to test it against. SUMIF and AVERAGEIF can also take a separate range to read the values from.
=SUMIF(A2:A6, "East", B2:B6)
=AVERAGEIF(A2:A6, "East", B2:B6)
=COUNTIF(B2:B6, ">100")These return 330, 110, and 2.
SUMIF(range, criteria, [sum_range])tests each cell inrange. For each match, it adds the cell in the same position ofsum_range.AVERAGEIF(range, criteria, [average_range])works the same way but averages the matching cells.COUNTIF(range, criteria)has no third argument. It counts the cells inrangethat match.
The first range is the one that gets tested. The last range is the one that gets read. They are different ranges in the first formula above, and the same range in the third.
Leaving out the third argument
If you omit sum_range, SUMIF tests and sums the same cells. This is the usual form when the condition is on the numbers themselves.
=SUMIF(B2:B6, ">100")
=AVERAGEIF(B2:B6, ">100")The first returns 270 (120 + 150). The second returns 135.
Writing criteria
The criterion is the part that causes most mistakes. It can be a number, text, a cell reference, or a text string that starts with a comparison operator.
| Criterion | Matches |
|---|---|
| `100` or `"=100"` | Cells equal to 100 |
| `"East"` | Cells equal to the text East (not case-sensitive) |
| `">100"` | Numbers greater than 100 |
| `">=100"` | Numbers greater than or equal to 100 |
| `"<>East"` | Cells that are anything other than East, including blanks |
| `"<>"` | Cells that are not empty |
| `"="` | Cells that are completely empty |
Anything with an operator must be in quotation marks. Writing >100 without them is a syntax error.
Using a cell as the criterion
Hard-coded criteria are hard to change. Put the criterion in a cell and refer to it. If E1 contains the text East, this formula needs no quotation marks:
=SUMIF(A2:A6, E1, B2:B6)To combine an operator with a cell value, join them with &. If E2 contains 100:
=COUNTIF(B2:B6, ">"&E2)The result is 2. The operator is text and E2 is a number, so & builds the string ">100" for you.
Wildcards in text criteria
Two wildcard characters work in text criteria:
*stands for any number of characters, including none.?stands for exactly one character.
=COUNTIF(A2:A6, "E*")
=COUNTIF(A2:A6, "?est")
=SUMIF(A2:A6, "*th", B2:B6)The first returns 3 (all the East rows). The second returns 1 (West). The third returns 95 (North). To match a literal * or ?, put a tilde in front of it, as in "~*".
Wildcards apply only to text matching. A criterion like "1*" does not match the number 120.
Behavior to know before you trust a result
Matching is not case-sensitive
"east", "East", and "EAST" all match the same cells. If you need an exact, case-sensitive match, these functions can't do it.
No match gives zero or an error
SUMIF and COUNTIF return 0 when nothing matches. AVERAGEIF has nothing to divide, so it returns #DIV/0!. You met this error with AVERAGE. The same guard works here:
=IFERROR(AVERAGEIF(A2:A6, "South", B2:B6), "No South sales")Use IFERROR only when you expect no matches. It also hides real errors, such as a mistyped range. A safer test uses COUNTIF first:
=IF(COUNTIF(A2:A6, "South")=0, "No South sales", AVERAGEIF(A2:A6, "South", B2:B6))Blank and text cells in the value range
The skipping rules from earlier in the tutorial still apply to the value range. SUMIF adds only numbers. AVERAGEIF ignores matching cells that are empty or contain text, so those cells don't pull the mean down. If every matching cell is empty or text, you get #DIV/0!.
An error value in a matching cell of the value range passes through to the result, as it does with plain SUM.
Numbers stored as text
A cell holding "150" as text does not behave like the number 150 in SUMIF's value range, and it is skipped. The criteria side is more forgiving. COUNTIF treats text "150" and the number 150 as equal when matching. This looks helpful, but it means COUNTIF can report matches that SUMIF then doesn't add. If a count and a sum disagree, check for text-formatted numbers.
Range shape
Keep the test range and the value range the same size and shape. Excel starts at the top-left cell of the value range and resizes it to match the test range. A mismatched value range therefore still returns an answer, but it may read cells you didn't intend. When the test range is A2:A6, use B2:B6 and not B2:B7.
Absolute references when copying
When you copy these formulas down a list of criteria, lock the ranges with $ and leave the criterion cell relative. With region names in D2:D4:
=SUMIF($A$2:$A$6, D2, $B$2:$B$6)Copied down, this becomes D3 and D4 in the criterion, while the ranges stay fixed on A2:A6 and B2:B6.
Criteria length
A text criterion longer than 255 characters returns a #VALUE! error. This rarely matters, but a criterion built by joining many strings can hit it.
Several conditions at once
SUMIF, AVERAGEIF, and COUNTIF take one condition. For several conditions, use the plural functions SUMIFS, AVERAGEIFS, and COUNTIFS. They apply every condition at once, so a cell must satisfy all of them.
=SUMIFS(B2:B6, A2:A6, "East", B2:B6, ">100")This returns 270, the East sales over 100.
In SUMIFS, the value range comes first and the test pairs follow. In SUMIF, it comes last. Mixing up the two orders is a common error.
When SUBTOTAL fits better
The conditional functions test cell contents. They do not look at whether a row is hidden or filtered out. If your question is "total only the rows I can see", the right tool is SUBTOTAL, covered in the next chapter.
Exercise
Using the sample data (Region in A2:A6, Sales in B2:B6), write one formula that returns the average East sale only when East appears in the data, and the text "No East sales" otherwise. What does it return for the sample data?
Show answer
=IF(COUNTIF(A2:A6, "East")=0, "No East sales", AVERAGEIF(A2:A6, "East", B2:B6)) returns 110, because East has sales of 120, 150, and 60, which total 330 across 3 rows.
Exercise
A colleague's formula =SUMIF(A2:A6, >100, B2:B6) shows an error. Name the mistake, then write a corrected formula that adds the sales in column B for the rows where column B is over 100.
Show answer
The criterion >100 is not in quotation marks, so it is a syntax error. Because the test and the values are in the same range, the corrected formula is =SUMIF(B2:B6, ">100"), which returns 270.