Knowledge base / Excel

COUNTIF in Excel: one condition, text and numeric thresholds

Six synthetic entries distinguish product occurrences from quantities and show when an asterisk is a pattern and when it is a literal character.

Kajo RudzińskiPublished Updated 6 min read

A single square gate selects cubes from a mixture of cubes and spheres.

When the question is how often a product appears in a column, I use COUNTIF. It applies one condition to one range and counts matching cells. I start by asking what a row represents. If it is an order line, the result counts those lines rather than automatically counting unique orders, customers or units sold. The unit belongs in the question before it appears in a chart title.

Syntax and the example table

With English names and comma separators:

=COUNTIF(range,criteria)

The criterion can be a value, text, a comparison or a cell reference. Microsoft documents COUNTIF for Microsoft 365, Excel for the web and versions including 2024, 2021, 2019 and 2016. Commas or semicolons depend on regional settings. The Polish name is LICZ.JEŻELI, and the Polish examples use semicolons. The separator convention does not alter the intended comparison.

Use the following synthetic table in A1:B7. Headers occupy row one, column B contains real numbers, and the asterisk in A7 is part of the product text.

Row A: Product B: Units
2 Coffee 2
3 Tea 1
4 coffee 3
5 Coffee (ground) 4
6 Cocoa 1
7 Coffee* 5

The row-number column is only a guide; do not insert it as an additional column before A. All formulas refer to the same small dataset so the reason for each result remains visible. The language versions translate the labels while preserving the same row structure and quantities.

Exact text does not mean case-sensitive text

=COUNTIF(A2:A7,"Coffee")

The result is 2, for A2 and A4. The function ignores letter case, as Microsoft explains in its usage guidance. Coffee (ground) and Coffee* do not match this criterion. A plain text criterion without wildcards is not an instruction to find any occurrence of that fragment within a longer name. The distinction matters when product labels share a prefix.

I do not interpret the result as a sum of coffee units. The two matching entries contain two and three units, but COUNTIF returns the number of cells. A question about quantity needs a different operation. Clarifying the unit before entering a formula avoids explaining later why a technically correct result answers the wrong business question. Counting an occurrence and adding its associated amount are separate tasks.

An asterisk can be a pattern or a literal character

Text criteria support * for any sequence of characters and ? for one character. A tilde lets you search for an actual asterisk or question mark. In this table:

=COUNTIF(A2:A7,"Coffee*")
=COUNTIF(A2:A7,"Coffee~*")

The first returns 4, covering rows 2, 4, 5 and 7. Each starts with Coffee regardless of letter case, including Coffee without a suffix. The second returns 1, because it requires a literal asterisk after the word. It matches row 7 only. That small tilde changes the criterion’s meaning, so I check it carefully when identifiers contain symbols. A character used in the data can also have a special role in a search expression.

A broad shape template matches several similar objects while a separate template selects one exact object.

Numeric comparisons and a threshold cell

To count entries containing more than two units:

=COUNTIF(B2:B7,">2")

The result is 3, for rows 4, 5 and 7. The operator and number form the criterion text, so they are enclosed in quotes. A value equal to two does not satisfy greater than two. Microsoft describes counting numbers or dates under a condition as a use of the function. The difference between greater than and greater than or equal to should be intentional.

If D2 contains the number 2 and I also want equality:

=COUNTIF(B2:B7,">="&D2)

The result is 4, covering rows 2, 4, 5 and 7. The operator is text, and & joins it to D2’s value. Writing ">=D2" does not insert that cell’s contents. If the threshold should stay fixed when copying the formula, I use $D$2; a fixed range becomes $B$2:$B$7. Protecting a reference against movement is a separate choice from selecting the comparison operator.

Adding counts can count the same row twice

Counts for mutually exclusive names can be added:

=COUNTIF(A2:A7,"Coffee")+COUNTIF(A2:A7,"Tea")

Here the result is 3. No cell is simultaneously the text Coffee and Tea. However, adding the count for Coffee* to the count for Coffee (ground) would include row 5 in both. The sum would not represent the number of rows satisfying at least one condition. Before adding separate counts, I check whether the matching groups overlap. An arithmetic sum does not automatically remove an intersection.

For conditions that must hold simultaneously in different columns, I move to COUNTIFS. Counting coffee entries with more than two units requires evaluating product and quantity in the same row. Two independent counts do not retain that relationship. Choosing between alternatives and requiring several conditions together are different logical questions, even when both involve more than one expression in ordinary language.

What I check when a result is surprising

I start with range addresses, the header and the kind of criterion. I then inspect spaces and extra characters in text. Formatting a cell does not remove stored characters. For numeric comparisons, I check that values are not merely text that looks like numbers. I do not try to repair every discrepancy by adding another operator before understanding the input. A small visible sample is useful for tracing which rows should match.

Microsoft also documents limits: matching strings longer than 255 characters can produce incorrect results, and a reference to a closed external workbook can lead to #VALUE!. The function does not count by background colour. When no cells match, zero is a normal result. It does not prove a formula error, but it also does not prove the criterion accurately expresses the business question. Both the data and the definition still need checking.

An exercise on the same data

Without changing the table, predict the result for ">=3" in column B and "Co?oa" in column A. The first gives three entries: three, four and five units. The second gives one, Cocoa. Then describe what each formula counts in words. If the description says number of units, revisit the unit: you counted matching cells rather than adding their values. The exercise is about preserving that distinction through the wording.

If you only need to check whether cells contain something, read COUNTA. I choose between functions by defining the result first. A short formula does not establish that I have counted the right thing, even when it is syntactically valid and returns a plausible number.

Key takeaways

Is COUNTIF case-sensitive?

No. Coffee matches both Coffee and coffee. If case is significant in an identifier, COUNTIF does not implement that comparison.

How do I reference a threshold in another cell?

Join the operator as text to the reference, for example ">="&D2. The string ">=D2" does not insert the value stored in D2.

Does COUNTIF sum the number of units sold?

No. It counts matching cells. Summing values under a condition needs a summing function, and counting under several simultaneous conditions needs COUNTIFS.

AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.

Kajo Rudziński

Newsletter KajoData

Let’s keep in touch.

I write about data analysis, learning and changing careers. My newsletter and the sign-up form are in Polish. If that works for you, I’d be glad to see you there.