The COUNTIF function in Excel counts the number of cells in a range that meet a single condition, such as counting how many sales are over $100 or how many times a name appears. Its syntax is =COUNTIF(range, criteria), where range is the group of cells to check and criteria is the condition, written as text, a number, an expression, or a cell reference.
What Is COUNTIF and When Would You Use It?
COUNTIF is one of the most frequently used functions in Excel because it answers a simple question: “how many cells in this list match a certain rule?” You might use it to count the number of products that are “In Stock,” the number of employees in a specific department, or the number of exam scores greater than 80. It works on numbers, text, dates, and even blank or non-blank cells, making it a versatile tool for summarizing data without manually scanning rows.
The function is part of the COUNT family, which includes COUNTA (counts non-empty cells), COUNTBLANK (counts empty cells), and COUNTIFS (counts cells meeting multiple conditions). COUNTIF is the simplest conditional counter, and it is available in all modern versions of Excel, including Excel for Microsoft 365, Excel 2021, Excel 2019, and older versions. It also exists in Google Sheets with the same syntax, though the menu paths for entering it differ slightly.
How COUNTIF Works: Syntax and Arguments
COUNTIF takes exactly two arguments:
- range – the group of cells you want to evaluate. This can be a single column, a row, or a rectangular block, like
A2:A100. - criteria – the condition that determines which cells are counted. This can be a number (e.g.,
5), a text string (e.g.,"Apples"), an expression (e.g.,">10"), or a reference to another cell that contains the value (e.g.,D1).
The criteria must be enclosed in quotes if it is text or an expression. For example, to count cells greater than 10, you write =COUNTIF(A2:A100, ">10"). If you use a cell reference in the criteria, you concatenate it with the operator, like =COUNTIF(A2:A100, ">"&B1). This is a common source of errors, because writing ">B1" treats “B1” as literal text, not a reference.
COUNTIF is not case-sensitive. So =COUNTIF(A2:A10, "apple") counts cells containing “Apple”, “APPLE”, or “apple”. It also counts partial matches if you use wildcards, which we’ll cover later.
COUNTIF vs. COUNTIFS: Which One Do You Need?
COUNTIF handles a single condition. If you need to count cells that meet two or more conditions at the same time, you must use COUNTIFS. For example, counting orders from the “West” region with a value over $500 requires two conditions, so COUNTIFS is the right tool:
=COUNTIFS(A2:A100, "West", B2:B100, ">500")
In this example, the formula counts rows where column A says “West” AND column B is greater than 500. COUNTIFS accepts multiple range/criteria pairs, up to 127 pairs in modern Excel. Each range must have the same number of rows and columns, otherwise the function returns a #VALUE! error.
A common misconception is that COUNTIF can count based on multiple criteria if you nest it, but that is not how it works. For example, =COUNTIF(A2:A100, "West", B2:B100, ">500") is invalid because COUNTIF only takes two arguments. You must use COUNTIFS for multiple conditions.
Using Wildcards with COUNTIF
COUNTIF supports three wildcard characters:
*(asterisk) – matches any sequence of characters. For example,=COUNTIF(A2:A10, "A*")counts cells that start with “A”.?(question mark) – matches any single character. For example,=COUNTIF(A2:A10, "B?t")counts “Bat”, “Bit”, “But”, etc.~(tilde) – escapes the wildcard, so you can count cells that contain an actual asterisk or question mark. For example,=COUNTIF(A2:A10, "~*")counts cells that contain a literal asterisk.
Wildcards are useful for counting partial matches, but they can cause unexpected results if your data contains characters like asterisks that you didn’t intend as wildcards. In that case, use the tilde to escape them.
Common Errors and How to Fix Them
Several errors are common when using COUNTIF:
- #VALUE! – This usually happens when the range argument is not a valid range, or when the criteria is a text string that is not properly quoted. For example,
=COUNTIF(A2:A10, apples)without quotes will cause an error because Excel interprets “apples” as a named range or formula. - Counting blanks or non-blanks – To count blank cells, use
=COUNTIF(A2:A10, ""). To count non-blank cells, use=COUNTIF(A2:A10, "<>"). Note that"<>"counts cells that are not empty, including cells with spaces or formulas that return an empty string. - Criteria with leading zeros – If you have numbers stored as text, like “00123”, COUNTIF may treat them differently. Use the criteria as text, e.g.,
=COUNTIF(A2:A10, "00123"), and ensure the cells are stored as text, not numbers. - Case sensitivity – COUNTIF is not case-sensitive. If you need a case-sensitive count, you must use SUMPRODUCT with EXACT, like
=SUMPRODUCT(--EXACT(A2:A10, "Apple")).
Real-World Examples of COUNTIF in Action
Let’s walk through a few practical examples with a small dataset. Suppose column A contains product names (A2:A10) and column B contains sales amounts (B2:B10).
- Count how many times “Widget” appears:
=COUNTIF(A2:A10, "Widget"). If “Widget” appears in A2, A5, and A7, the result is 3. - Count sales greater than $100:
=COUNTIF(B2:B10, ">100"). If B2, B4, and B9 are above 100, the result is 3. - Count sales that are exactly $50:
=COUNTIF(B2:B10, 50)– note that the number 50 does not need quotes. - Count products that start with “W”:
=COUNTIF(A2:A10, "W*"). - Count cells that are not empty in column B:
=COUNTIF(B2:B10, "<>").
These examples show how COUNTIF can quickly summarize data without using pivot tables or manual sorting.
COUNTIF in Google Sheets and Excel for the Web
COUNTIF works identically in Google Sheets, but the way you enter it differs slightly. In Google Sheets, you type the formula directly into a cell, and it uses the same syntax. However, Google Sheets has a separate function called COUNTIFS as well, but it also supports an optional third argument for additional criteria in some versions – though that is not standard. For most users, the syntax is the same.
In Excel for the web (the browser version), COUNTIF works exactly as in desktop Excel. The only difference is that you might find the function in the Formula tab, but you can also just type it manually. On Mac, the function is identical, and keyboard shortcuts like Ctrl+Z (undo) are replaced by Cmd+Z.
One notable difference: in Google Sheets, you can use COUNTIF with a range that includes multiple sheets by using the INDIRECT function, but that’s an advanced technique. For most counting tasks, the basic function is enough.
Performance and Large Data Sets
COUNTIF is generally fast, even on large datasets. However, if you use it on thousands of rows with complex criteria, it can slow down your workbook. For very large data, consider using a pivot table or the newer dynamic array functions like FILTER combined with COUNTA, but for typical use, COUNTIF is fine.
One performance tip: avoid using entire column references like A:A if you only need a specific range, because that forces Excel to check every cell in the column, which can be slower. Use a defined range like A2:A1000 instead.
Frequently Asked Questions
Can COUNTIF count cells based on the color of the cell? No, COUNTIF cannot evaluate cell formatting like fill color or font color. To count colored cells, you need to use VBA or the SUBTOTAL function with filters, or use the newer CELL function which can return some formatting info, but that is not standard. A common workaround is to add a helper column that marks the color, then use COUNTIF on that column.
How do I count cells that contain a specific text but not exactly that text? Use wildcards. For example, =COUNTIF(A2:A10, "*apple*") counts any cell that contains the word “apple” anywhere. If you want to count cells that start with “apple”, use "apple*".
Why does my COUNTIF return 0 even though I can see matching cells? This often happens when the criteria is a text string but the cells contain numbers, or vice versa. For example, if you count "123" but the cells contain the number 123, they won’t match. Also, check for extra spaces in the cells or criteria. Use the TRIM function or clean the data.
Can I use COUNTIF with dates? Yes. For example, to count dates after January 1, 2024, use =COUNTIF(A2:A10, ">"&DATE(2024,1,1)). You can also use a cell reference that contains the date. Dates are stored as numbers in Excel, so comparisons work.
Is there a way to count unique values with COUNTIF? Not directly. COUNTIF counts occurrences, not unique items. To count unique values, you can use the newer UNIQUE function combined with COUNTA, like =COUNTA(UNIQUE(A2:A10)), but that requires Excel 365 or Excel 2021. Alternatively, you can use SUMPRODUCT with COUNTIF to get a unique count, but that is more complex.
Alternatives to COUNTIF
If you need more flexibility, consider these alternatives:
- COUNTIFS – for multiple conditions.
- SUMPRODUCT – for array-based counting, especially when you need to use functions like EXACT for case-sensitive counts or when you need to count based on multiple criteria that involve OR logic.
- FILTER + COUNTA – in Excel 365, you can filter a range and count the results, which is useful for dynamic arrays.
- PivotTables – for interactive counting and grouping, especially with large datasets.
Each method has its strengths, but COUNTIF remains the simplest for basic conditional counting.
Related
- Format Excel Cell So Leading Zero Shows
- Google Sheets Query Label: How to Use It
- Random QR Code: How to Get One Safely
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Subtotal and COUNTIF Combined in ExcelYes, you can combine SUBTOTAL and COUNTIF in Excel, but not directly in a single formula. The standard…
- Excel Formula for Percentage Increase: Examples and TipsTo calculate percentage increase in Excel, subtract the old value from the new value, then divide by the…
- How to Use VLOOKUP in Excel: Step-by-StepVLOOKUP is Excel’s classic lookup function that searches for a value in the first column of a table…
- Excel SUMPRODUCT with IF: How to Use ItThe direct answer: Excel’s SUMPRODUCT function can perform conditional summing without needing an IF function, by multiplying a…
- Excel Data Validation Custom Formula: Rules and ExamplesIn Excel for Microsoft 365 on Windows, select the cells to control, choose Data > Data Validation, set…
- How to Add a Formula to an Entire Row in ExcelTo add a formula across a row in Excel, enter it in the first cell where you want…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.