Statistical functions

COUNTIF

Matches all 103 recorded answers

Recorded: 103 workbook cells. How we measure this · See the Rust code

Local names in 40 languages
LanguageName in Excel
ArabicCOUNTIF
BulgarianCOUNTIF
CzechCOUNTIF
DanishTÆL.HVIS
GermanZÄHLENWENN
GreekCOUNTIF
English (Australia)COUNTIF
English (United Kingdom)COUNTIF
English (United States)COUNTIF
SpanishCONTAR.SI
EstonianCOUNTIF.
FinnishLASKE.JOS
FrenchNB.SI
HebrewCOUNTIF
CroatianCOUNTIF
HungarianDARABHA
IndonesianCOUNTIF
ItalianCONTA.SE
JapaneseCOUNTIF
KoreanCOUNTIF
LithuanianCOUNTIF
LatvianCOUNTIF
NorwegianANTALL.HVIS
DutchAANTAL.ALS
PolishLICZ.JEŻELI
Portuguese (Brazil)CONT.SE
Portuguese (Portugal)CONTAR.SE
RomanianCOUNTIF
RussianСЧЁТЕСЛИ
SlovakCOUNTIF
SlovenianCOUNTIF
Serbian (Latin)COUNTIF
SwedishANTAL.OM
ThaiCOUNTIF
TurkishEĞERSAY
UkrainianCOUNTIF
VietnameseCOUNTIF
Chinese (Simplified)COUNTIF
Chinese (Hong Kong)COUNTIF
Chinese (Traditional)COUNTIF

Names from Microsoft's support site.

What it does

COUNTIF(range, criteria)

COUNTIF(range, criteria) counts cells satisfying one condition. The criterion can be a number, text, a cell containing the condition, or a comparison such as ">=10".

Try it

Input cells (optional)

Example: {"B2": 3, "B3": 4}. A1 holds the formula. Other cells need values before a formula can read them.

Enter a formula, then calculate.

Runs locally in your browser with OneCalc. Its answer may differ from Excel. Use English function names and commas between arguments.

Put 4,11,7,13 in B2:B5:

FormulaCountWhy
=COUNTIF(B2:B5,">=10")2Eleven and thirteen qualify
=COUNTIF(B2:B5,">"&B4)2B4 supplies seven, so the criterion becomes ">7"
=COUNTIF(B2:B5,">=7")-COUNTIF(B2:B5,">11")2Include seven through eleven and remove larger values

For text, put sku-1, SKU-2, sku-*, other in D2:D5. =COUNTIF(D2:D5,"sku-?") counts three: case is ignored and the question mark accepts any one final character. =COUNTIF(D2:D5,"sku-~*") counts one, matching the literal asterisk.

Quirks

Text matching is case-insensitive. * matches a sequence and ? one character; prefix either with ~ to match it literally. Operators belong inside quoted criteria. To use an operator with a cell’s value, concatenate them with &; placing the cell address inside the quoted text does not dereference it.

A numeric equality test and a numeric ordering test are different predicates. The current matcher can convert numeric-looking text for equality, while ordering criteria compare numeric cells. Preserve cell kinds through matching instead of coercing the whole range to numbers first.

Microsoft documents misleading results when matching strings longer than 255 characters and #VALUE! for calculated references into a closed workbook. Leading or trailing spaces can also change a text match. The current implementation’s criteria and Unicode coverage limits are separate from those documented worksheet behaviours.

How to implement

  1. Resolve a single-area range reference. The current implementation requires a reference; a supplied array value is a distinct route and is not silently turned into one.
  2. Read the criterion once. Dereference a one-cell criterion reference. Keep numeric, logical, error, empty and text criteria as different kinds; an empty criterion cell becomes numeric zero.
  3. For text, recognize the longest leading operator first: <=, >=, <>, then <, >, =. Missing operator means equality. Parse the remaining operand as an error literal, a wildcard pattern, an admitted number or logical, or ordinary text under the current rules.
  4. Match each cell with that typed predicate and count matches. Patterns need *, ? and tilde escaping with the supported case mapping. Blank cells and empty strings must remain distinct until the predicate chooses their verdict.
  5. Return the count as a number. Do not evaluate the criterion string as arbitrary formula code.

For the numeric example, four and seven fail >=10, while eleven and thirteen pass. The interval expression counts three values at least seven, then subtracts the one above eleven, leaving two.

The current code limits criterion text to 255 UTF-16 code units and refuses some unresolved collation, text-coercion and array-criterion cases. A typed implementation refusal reports an unsupported case; it is not evidence that Excel returns a worksheet error there. Error-valued criteria also need their own predicate handling rather than an unconditional exception path.

Rust code

Show the CalcFunc source of this function

Quoted from the public repository DnaCalc/DnaCalc, crates calc-func and calc-fml, at commit 9e1884369a67. Comments are the source's own. A kernel shared with other functions appears once, on its module page.

COUNTIF · 1 lines · calc-func/src/catalogue/conditional.rs on GitHub
conditional(COUNTIF, "COUNTIF", (2, 2), RANGE_CRITERIA, countif)
countif · 11 lines · calc-func/src/catalogue/conditional.rs on GitHub
/// COUNTIF: the cells of the range that meet the criterion.
fn countif(
    arguments: &[CalcValue],
    services: &mut EvalServices<'_>,
) -> Result<CalcValue, FunctionFault> {
    let site = Site::new(COUNTIF, services);
    Site::finish(single(&site, arguments, services, false).and_then(|plan| {
        let conditions = plan.read_conditions(&site, services)?;
        count(&site, &conditions, plan.size)
    }))
}

Kernels shared with other functions

Argument and result plumbing

No implementation has been published with a Handbook test suite for this function.

Version differences

No same-input comparison between named Excel builds is recorded for this function here. The documented behaviour below is not a measurement of every Excel release.

Alternatives

COUNTIFS combines conditions with AND. For OR conditions, account for overlap when adding separate counts. FILTER returns matching rows.

Named variants

No named variants are recorded yet.

Suggest a correction or contribute an implementation · For agents