Spreadsheet Practice

Why COUNTIF returns 0 when the value is there

Formula (Excel and Google Sheets)

=SUMPRODUCT(--(TRIM(A2:A10)=E1))

Hidden spaces are the usual cause. Compare trimmed values: =SUMPRODUCT(--(TRIM(A2:A10)=E1)).

Result on the example
3 in E2
Works in
Every current version
Google Sheets
Same formula

See it work

The formula is already in E2. Change any input and the result updates, or edit the formula to experiment.

How it works

The export added spaces before or after some names, so "Contoso " is not equal to "Contoso" and =COUNTIF(A2:A10,E1) returns 0. TRIM removes leading and trailing spaces from each name before the comparison, and SUMPRODUCT counts the matches: 3.

The lasting fix is to clean the column once (a TRIM helper column, pasted back as values) so every formula works.

=SUMPRODUCT(--(TRIM(A2:A10)=E1))

Result3

  1. A2:A109 rows × 1 col: "Contoso ", "Fabrikam", " Contoso", "Litware ", …

    A range: 9 cells in one column.

  2. TRIM(A2:A10){9 rows × 1 col} "Contoso", "Fabrikam", "Contoso", "Litware", …

    TRIM: Removes extra spaces, leaving single spaces between words.

    • textA2:A109 rows × 1 col: "Contoso ", "Fabrikam", " Contoso", "Litware ", …
  3. E1"Contoso"

    The value in E1.

  4. TRIM(A2:A10)=E1{9 rows × 1 col} TRUE, FALSE, TRUE, FALSE, …

    The = operator tests whether the two sides are equal.

  5. -(TRIM(A2:A10)=E1{9 rows × 1 col} -1, 0, -1, 0, …

    Makes the value negative.

  6. --(TRIM(A2:A10)=E1{9 rows × 1 col} 1, 0, 1, 0, …

    Makes the value negative.

  7. SUMPRODUCT(--(TRIM(A2:A10)=E1))3

    SUMPRODUCT: Multiplies arrays row by row and adds the results.

    • array1--(TRIM(A2:A10)=E1{9 rows × 1 col} 1, 0, 1, 0, …

In Google Sheets

The same formula works in Google Sheets: =SUMPRODUCT(--(TRIM(A2:A10)=E1)). We ran it in Google Sheets mode on the same data and it returns 3.

Other ways to do it

Each one was run on the same example sheet.

FormulaResultWhen to use it
=COUNTIF(A2:A10,"*"&E1&"*")3Wildcards tolerate the spaces, but they also count longer names that contain the text, like "Contoso Ltd".
=COUNTIF(A2:A10,E1)0Different resultThe formula that fails on this data. Returns 0 because every Contoso has a stray space.

Common mistakes

  • Text copied from web pages often contains non-breaking spaces (CHAR(160)), which TRIM doesn't remove. Use TRIM(SUBSTITUTE(A2:A10,CHAR(160)," ")).
  • Numbers stored as text don't match a numeric criteria in some functions. If the column is IDs, check with =ISTEXT(A2).
  • A criteria with an operator must be in quotes: ">"&E1, not >E1.

Try it yourself

Same data, empty answer cell. Your formula is graded instantly, then re-checked on changed data, so a typed-in number won't pass.

Why COUNTIF returns 0 when the value is there

In E2, count the rows for the customer in E1, ignoring stray spaces in the export.

Questions

How do I check whether two cells really match?

Type =A2=E1 in a spare cell. FALSE means something differs; =LEN(A2) shows hidden extra characters.

Why does COUNTIF return 0 for numbers?

Usually the criteria is inside the quotes as text, like ">E1", or the numbers are stored as text. Fix the quotes or convert the column.

Checked by running on 2026-10-30: every formula and result on this page was calculated by the Spreadsheet Practice formula engine, in both Excel and Google Sheets modes.