Sub topic 13
Next step: SUMIF, AVERAGEIF, and COUNTIF
This chapter assumes you can write SUM, AVERAGE, and COUNT over a range. It also assumes you know how relative and absolute references behave when a formula is copied. Here you add a condition, so each function reads only the cells that meet a test.
Why add a condition
SUM, AVERAGE, and COUNT process every number in the range you give them. Real questions are usually narrower, such as "total sales for the East region" or "how many orders exceed 100". The conditional versions answer these without a helper column or a manual filter.
The examples below use this data:
| Row | A (Region) | B (Sales) |
|---|---|---|
| 2 | East | 120 |
| 3 | West | 80 |
| 4 | East | 95 |
| 5 | North | 150 |
| 6 | East | 60 |
The three functions
=SUMIF(A2:A6, "East", B2:B6)
=AVERAGEIF(A2:A6, "East", B2:B6)
=COUNTIF(B2:B6, ">100")The results are 275, about 91.67, and 2.
Each function has a different argument layout:
SUMIF(range, criteria, [sum_range])testsrangeand adds the matching cells ofsum_range.AVERAGEIF(range, criteria, [average_range])follows the same pattern and averages the matching cells.COUNTIF(range, criteria)has no third argument. It counts the cells inrangethat pass the test.
If you leave out the third argument of SUMIF or AVERAGEIF, the function tests and totals the same range. =SUMIF(B2:B6, ">100") adds only the values above 100, which gives 270.
Writing criteria
The criteria argument is flexible, and its syntax is a common source of errors.
- Text and numbers:
"East"or100match exactly. Matching ignores upper and lower case. - Comparisons: put the operator inside the quotes, as in
">100","<=50", or"<>East". - Wildcards:
"E*"matches any text starting with E, and"?ast"matches one unknown character. - Values from a cell: join the operator and the reference with
&.
=COUNTIF(B2:B6, ">"&D1)If D1 holds 100, this formula counts values above 100. Changing D1 changes the result, so you don't need to edit the formula.
Common errors
- Ranges of different sizes. Keep
rangeandsum_rangethe same shape. Excel anchors the sum range at its top-left cell and resizes it to match, so a mismatched range can silently sum the wrong cells. - Operator outside the quotes.
=COUNTIF(B2:B6, >100)is a syntax error. Write">100". - Numbers stored as text. A cell holding the text "100" may not match a numeric criterion. Convert the data first.
- No matching cells. AVERAGEIF returns
#DIV/0!when nothing matches, the same as AVERAGE on an empty range. SUMIF and COUNTIF return 0 instead.
A guard for the fourth case counts the matches first:
=IF(COUNTIF(A2:A6, "South")=0, "No data", AVERAGEIF(A2:A6, "South", B2:B6))When one condition is not enough
These three functions take a single test. To apply several tests at once, such as East and above 100, use SUMIFS, AVERAGEIFS, and COUNTIFS. They put the sum or average range first, then take pairs of range and criteria.
Exercise
Using the data above, write one formula that counts how many sales in column B belong to the East region and one that returns the average sale for the North region. Which of the two could return #DIV/0!?
Show answer
The first formula is =COUNTIF(A2:A6, "East"), which returns 3. The second is =AVERAGEIF(A2:A6, "North", B2:B6), which returns 150. AVERAGEIF can return #DIV/0! if no row matches, for example when the region name is misspelled. COUNTIF returns 0 in that case.