- Purpose
- Count the cells in a range that meet one condition.
- Returns
- A whole number. 0 if no cell meets the condition.
- Syntax
- Works in
- Every version of Excel · Microsoft 365 · Google Sheets · LibreOffice Calc
Arguments
| Argument | Required | What it is |
|---|---|---|
| range | Yes | The cells to check. |
| criteria | Yes | The condition a cell must meet to be counted. A number, a cell reference, or text in double quotes such as "Done" or ">=50". |
Example: count the tasks with one status
Each cell in column C is compared with the status in E2. Three cells contain Done, so the result is 3.
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Task | Owner | Status | Status | Count | |
| 2 | Draft brief | Mei | Done | Done | 3 | |
| 3 | Book venue | Sam | Open | |||
| 4 | Send invites | Mei | Done | |||
| 5 | Order badges | Jo | Blocked | |||
| 6 | Print signs | Sam | Done | |||
| 7 | Confirm AV | Jo | Open |
Build your COUNTIF formula
Fill in the fields with your own cells. The formula updates as you type.
Counts the cells in A2:A20 that meet the condition "Done".
Paste the formula into cell E2 or any empty cell. The coloured parts match the coloured fields. Using a different list separator? Replace the commas with semicolons.
How COUNTIF works
COUNTIF looks at each cell in the range, tests it against the criteria, and adds 1 to the result for every cell that passes. The answer is a count of cells. The values in those cells are never added together.
There are two arguments. The range is where to look. The criteria is the test. Everything that makes COUNTIF flexible is in how you write that second argument.
Writing the criteria
A plain number or a cell reference needs no quotes. Text, and any condition with a comparison operator, must be inside double quotes.
| Criteria | Counts |
|---|---|
"Done" |
Cells equal to Done. Case is ignored. |
50 |
Cells equal to 50. |
">100" |
Numbers greater than 100. |
"<=100" |
Numbers less than or equal to 100. |
"<>Done" |
Cells that are not equal to Done, blank cells included. |
"*east*" |
Text that contains “east” anywhere. |
"???" |
Text that is exactly three characters long. |
E1 |
Cells equal to the value in E1. |
">="&E1 |
Numbers greater than or equal to the value in E1. |
"" |
Blank cells. |
"<>" |
Cells that are not blank. |
To compare against a cell, leave the operator in quotes and join the reference with &. The cell name must stay outside the quotes.
Two wildcards work in text criteria. * stands for any run of characters, including none. ? stands for exactly one character. To count cells that contain a real asterisk or question mark, put a tilde in front: "*~?*" counts cells with a question mark in them.
More examples
The first three use the example data above, with tasks in column A, owners in column B and statuses in column C.
Everything that is not finished
=COUNTIF(C2:C7, "<>Done")
returns 3, the two Open tasks and the one Blocked task.
Text that contains a fragment
=COUNTIF(A2:A7, "*in*")
returns 2, because “Send invites” and “Print signs” both contain “in”.
Either of two values
=SUM(COUNTIF(C2:C7, {"Done","Open"}))
returns 5. COUNTIF produces one count for each value in the list, 3 and 2, and SUM adds them.
Count distinct values
=SUMPRODUCT(1/COUNTIF(A2:A10, A2:A10))
returns how many different values A2:A10 holds. The range must have no blank cells, or the formula returns #DIV/0!.
Tips
- Lock the range with dollar signs (
$C$2:$C$7) when you copy the formula down next to a list of values to count. - Trailing spaces break exact matches. “Done “ with a space is a different value from “Done”.
- COUNTIF matches only the first 15 digits of a long number. Two 16-digit IDs that differ in the last digit count as the same value, even when they are stored as text.
- A number and the same number stored as text are counted together by an equality test.
=COUNTIF(A2:A10, 50)counts both 50 and the text “50”. =COUNTIF(A2:A10, E2)>0is a quick test for “is this value in the list”. It returns TRUE or FALSE.- For a one-off answer you do not need a formula. The count unique values tool and the remove duplicates tool work on a pasted or opened file.
COUNTIF or COUNTIFS
COUNTIF tests one range against one condition. COUNTIFS takes pairs of ranges and conditions, and counts a row only when every pair is true.
| You need | Use |
|---|---|
| One condition | =COUNTIF(C2:C7, "Done") |
| Two conditions that must both be true | =COUNTIFS(C2:C7, "Done", B2:B7, "Mei") |
| A number between two bounds | =COUNTIFS(D2:D50, ">=10", D2:D50, "<=20") |
| Either of two values | =COUNTIF(C2:C7, "Done") + COUNTIF(C2:C7, "Open") |
The argument order is the same in both: range first, then criteria. COUNTIFS with a single pair gives the same answer as COUNTIF. COUNTIFS needs Excel 2007 or later, and COUNTIF works in every version.
To add up the matching values and not count them, use SUMIF.
Errors and fixes
| Error | Why it happens | Fix |
|---|---|---|
| Wrong count | The values are numbers longer than 15 digits, such as card or serial numbers. COUNTIF compares only the first 15 digits, so different numbers can count as equal. | Use =SUMPRODUCT(--(A2:A100=E2)), which compares the full text. |
| 0 result | Nothing matched. Common causes are trailing spaces in the cells and a cell name typed inside the quotes, as in ">E1". | Clean the spaces with TRIM, and join cell references with an ampersand: ">"&E1. |
| Count too high | The criteria text contains * or ?, which COUNTIF reads as wildcards. | Put a tilde before the character to match it literally: "~?". |
| #VALUE! | The formula refers to a range in another workbook that is closed. | Open the other workbook, or copy the data into the same file. |
Questions
Is COUNTIF case-sensitive?
No. "done", "Done" and "DONE" are counted as the same value. For a case-sensitive count use =SUMPRODUCT(--EXACT(C2:C7, "Done")).
Can COUNTIF use multiple criteria?
No, it takes one condition. When every condition must be true, use COUNTIFS. When either of two values should be counted, add two COUNTIF formulas: =COUNTIF(C2:C7, "Done") + COUNTIF(C2:C7, "Open").
How do I count cells that contain specific text?
Put asterisks around the text: =COUNTIF(A2:A100, "*invoice*") counts every cell that contains "invoice" anywhere. With the text in a cell, write "*"&E1&"*".
How do I count blank or non-blank cells with COUNTIF?
=COUNTIF(A2:A100, "") counts blank cells, including cells with a formula that returns empty text. =COUNTIF(A2:A100, "<>") counts cells that are not blank.
How do I find duplicates with COUNTIF?
In a helper column, enter =COUNTIF($A$2:$A$100, A2)>1 and copy it down. It shows TRUE on every row whose value appears more than once. To do it without a formula, use the find duplicates tool.
Does COUNTIF count text and numbers?
Yes. It counts any cell that meets the condition, whether the cell holds text, a number or a date. Comparison conditions such as ">50" count numbers only.