Why is COUNTIF not counting cells with extra spaces?
Formula (Excel and Google Sheets)
=SUMPRODUCT(--(TRIM(B2:B9)=E1))COUNTIF compares whole cells, so " billing" isn't "billing". Trim inside the count: =SUMPRODUCT(--(TRIM(B2:B9)=E1)).
- Result on the example
- 5 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
Some tags were typed or imported with spaces before or after the word, and COUNTIF compares whole cells exactly, so it finds only the 2 clean "billing" cells. TRIM(B2:B9) strips the spaces inside the formula, and SUMPRODUCT counts the trimmed tags that match: 5.
=SUMPRODUCT(--(TRIM(B2:B9)=E1))Result5
B2:B98 rows × 1 col: "billing", "billing ", "login", " billing", …A range: 8 cells in one column.
TRIM(B2:B9){8 rows × 1 col} "billing", "billing", "login", "billing", …TRIM: Removes extra spaces, leaving single spaces between words.- text
B2:B98 rows × 1 col: "billing", "billing ", "login", " billing", …
- text
E1"billing"The value in E1.
TRIM(B2:B9)=E1{8 rows × 1 col} TRUE, TRUE, FALSE, TRUE, …The = operator tests whether the two sides are equal.
-(TRIM(B2:B9)=E1{8 rows × 1 col} -1, -1, 0, -1, …Makes the value negative.
--(TRIM(B2:B9)=E1{8 rows × 1 col} 1, 1, 0, 1, …Makes the value negative.
SUMPRODUCT(--(TRIM(B2:B9)=E1))5SUMPRODUCT: Multiplies arrays row by row and adds the results.- array1
--(TRIM(B2:B9)=E1{8 rows × 1 col} 1, 1, 0, 1, …
- array1
In Google Sheets
The same formula works in Google Sheets: =SUMPRODUCT(--(TRIM(B2:B9)=E1)). We ran it in Google Sheets mode on the same data and it returns 5.
Other ways to do it
Each one was run on the same example sheet.
| Formula | Result | When to use it |
|---|---|---|
| =COUNTIF(B2:B9,"*"&E1&"*") | 5 | Wildcards on both sides tolerate the spaces, but also count longer tags that contain the word. |
Common mistakes
- TRIM doesn't remove the non-breaking spaces that come from web pages. Add
SUBSTITUTE(B2:B9,CHAR(160),"")inside. - COUNTIF ignores case, so case isn't why it misses cells. Spaces and numbers stored as text are the usual causes.
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 is COUNTIF not counting cells with extra spaces
E2, count the tickets tagged with the word in E1, ignoring spaces before or after the tag.Questions
How do I clean the column for good?
Helper column =TRIM(B2), then Paste Values over the original.
How do I count every tag in a list?
Put the tags in a column and fill =SUMPRODUCT(--(TRIM($B$2:$B$9)=D2)) down next to them.
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.