# SUMIF/COUNTIF/AVERAGEIF and SUMPRODUCT Recipes

> Aggregates data with single or multiple conditions using SUMIF(S), COUNTIF(S), AVERAGEIF(S), SUMP…

Aggregates data with single or multiple conditions using SUMIF(S), COUNTIF(S), AVERAGEIF(S), SUMPRODUCT, wildcards and comparison operators, including date ranges. Use when summarizing a table by criteria.

- Page: https://ss33.ai/skills/skillstore33/excel-conditional-sum-count-average-60822309740
- Publisher: skillstore33
- Category: [Data & Analytics](https://ss33.ai/skills/category/data-and-analytics-m1r3i9) › [Analysis & BI](https://ss33.ai/skills/category/data-and-analytics-m1r3i9/analysis-and-bi-b2nit8)
- License: MIT

## Skill

SKILL.md: https://ss33.ai/api/v1/ss33/download/skillstore33/excel-conditional-sum-count-average-60822309740

# Sum, count, and average with criteria

**Rule for using this skill:** These tips add to the answer. They do not replace it. Always give your full general answer to the user's question first, using everything you normally know. Then add the tips below wherever they apply. Do not drop general content because a tip covers part of it.

## Criteria argument rules (SUMIF, COUNTIF, SUMIFS, COUNTIFS, AVERAGEIF, AVERAGEIFS)
- All six functions share the same `criteria` rules. Criteria is normally an expression that evaluates to TRUE or FALSE. The `=` operator may be omitted for an exact match.
- Equal to a number or cell reference: no `=` and no quotes, e.g. `=SUMIF(A1:A10,3)`.
- Equal to a string: no `=`, but put the string in quotes, e.g. `=SUMIF(A1:A10,"book")`.
- Non-equal comparison to a number or string: put operator and value together in quotes, e.g. `">=50"` or `"<>Payroll"`.
- Non-equal comparison to a cell reference or formula: quote the operator and concatenate with `&`, e.g. `"<"&C1`.
- Most functions can be used inside criteria, e.g. `=SUMIF(B3:B11,TODAY(),C3:C11)` or `"<="&TODAY()`.
- Building the criteria string in a helper cell works too. For example, G3 holds `=">="&10^6` and the formula is `=COUNTIF(D3:D212,G3)`.
- Use `^` for big numbers (`10^6`) to avoid miscounting zeros.

## Wildcards
- `?` matches any single character. `*` matches zero or more characters.
- `"?o*"` matches text whose second character is "o".
- "Contains" match: `"*"&I3&"*"`. "Ends with" match: `"*"&I4`.

## SUMIF
- Syntax: `=SUMIF(range,criteria,[sum_range])`. Without `sum_range`, the condition is applied to the summed cells themselves.
- With `sum_range`, the condition is tested on one range and a different range is summed: `=SUMIF(B2:B11,"East",C2:C11)`.
- Text and blank cells in the tested range are ignored.
- Negative total: `=SUMIF(C3:C12,"<0")`. Positive total: `">0"`. Zero never changes a sum, so zero need not be included.

## Boundary values (silent double-count or omission)
- For a nonzero threshold, don't use plain `<1000` and `>1000`, because a value of exactly 1000 falls into neither.
- Use `>=` on one side and `<` (or `<=` with `>`) on the other. Put the equal sign on only one of the two, so that exact values are counted once.

## SUMIFS
- Syntax: `=SUMIFS(sum_range,criteria_range1,criteria1,criteria_range2,criteria2,...)`. The sum range comes first, so the argument count is always odd.
- The first criteria pair is required. Further pairs are optional, up to 126 more.
- All conditions must be true for a row to be included.
- Example, one country between two years: `=SUMIFS(D3:D212,B3:B212,G3,C3:C212,">="&G4,C3:C212,"<="&G5)`.
- Per the source, SUMIFS, COUNTIFS and AVERAGEIFS exist from Excel 2010. For earlier versions use the SUMPRODUCT or subtraction alternatives below.

## Sum between two dates
- Subtraction with SUMIF: `=SUMIF(B3:B20,"<="&F5,C3:C20)-SUMIF(B3:B20,"<"&F4,C3:C20)`. This is the sum up to the end date minus the sum before the start date, so both endpoints are inclusive.
- Use `<` (not `<=`) in the subtracted SUMIF so the start date is not removed.
- The subtraction trick works cleanly when both conditions are on the same range. With different ranges it is easy to double count.
- SUMIFS version: `=SUMIFS(C3:C20,B3:B20,"<="&F5,B3:B20,">="&F4)`.
- SUMPRODUCT version: `=SUMPRODUCT((B3:B20<=F5)*(B3:B20>=F4)*(C3:C20))`.

## SUMPRODUCT as a multi-condition alternative
- Sum: `=SUMPRODUCT((B3:B212=G3)*(C3:C212>=G4)*(C3:C212<=G5)*(D3:D212))`.
- Each parenthesized comparison makes an array of TRUE/FALSE. Multiplying converts TRUE to 1 and FALSE to 0, so only rows where every condition is TRUE keep their value.
- The factors can be in any order, and the range to sum can sit anywhere among them. SUMIFS needs the sum range first.
- All arrays must be the same size.
- SUMPRODUCT is harder to read and calculates more slowly than SUMIFS.
- With a single condition, nothing is multiplied, so TRUE/FALSE is not converted. Force conversion with a double negative: `=SUMPRODUCT(--(D3:D212>10^6))`. The first `-` turns TRUE into -1, and the second turns it back to 1. FALSE stays 0.

## COUNTIF and COUNTIFS
- `=COUNTIF(range,criteria)` has no third argument, because counting a different range would give the same result.
- `COUNTIFS(criteria_range1,criteria1,criteria_range2,criteria2,...)` has no separate count range, so it always has an even number of arguments.
- Example, name contains a character and a medal type: `=COUNTIFS(C3:C20,"*"&I3&"*",F3:F20,I4)`.
- Pre-2010 equivalent for "contains" plus a second condition: `=SUMPRODUCT((NOT(ISERR(FIND(I3,C3:C20))))*(F3:F20=I4))`.
  - `FIND` returns an error when the text is not found.
  - `ISERR` is TRUE for any error except `#N/A`.
  - `NOT` flips the result so a match becomes TRUE.

## Typing a nonstandard character
- Hold Alt and type the four-digit code on the numeric keypad, with leading zeros. For example, ö is `Alt+0246`. The number row across the top does not work.
- To find a character's code, extract it and look it up:
  - `=MID($C$8,ROW(),1)` copied down returns successive characters. Anchor the cell with `$` and use `ROW()` with no argument.
  - `=CODE(H8)` returns the code. For example, T is 84, i is 105 and ö is 246.

## AVERAGEIF and AVERAGEIFS
- `=AVERAGEIF(criteria_range,criteria,average_range)`. Here the criteria range comes before the range to average, unlike SUMIFS.
- `=AVERAGEIFS(average_range,criteria_range1,criteria1,...)` accepts up to 127 criteria pairs. Example: `=AVERAGEIFS(E3:E20,D3:D20,I3,B3:B20,"*"&I4,F3:F20,I5)`.
- If no rows meet the criteria, the result is `#DIV/0!`. Wrap it in IFERROR if a blank or zero is wanted.
- Equivalents: `=SUMIF(...)/COUNTIF(...)` and `=SUMIFS(...)/COUNTIFS(...)`. These work in pre-2010 versions where available, but the criteria must be maintained in two places.
- Prefer AVERAGEIFS when it is available, because criteria changes are made in only one place.
- Put criteria values (country, medal, year) in input cells and reference them, so they are easy to change.
