Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

When COUNTIF returns a smaller number than the rows you can see, a trailing space is one possible hidden cause. A cell that reads Smith and a cell that reads Smith look the same on screen, but COUNTIF treats them as different text unless the criterion accounts for the extra character. The fix is to find the stray characters, normalize them in a helper column, and count the cleaned values. The steps below apply to Microsoft Excel and Google Sheets, with the differences noted where they matter.

How COUNTIF compares text

In both Excel and Google Sheets, COUNTIF(range, criterion) counts the cells in a range that meet a condition. For a plain text criterion, the function checks each cell for equality with that text. Google’s COUNTIF help describes this equality matching and explains that wildcards change it. Microsoft’s COUNTIF guidance warns that leading spaces, trailing spaces, inconsistent quotation marks, and nonprinting characters can make the function return an unexpected result.

Both products document COUNTIF as not case-sensitive. That means a difference in capitalization alone is an unlikely explanation for an undercount. Spaces and invisible characters are the more common suspects, because the comparison is exact: every character in the cell and in the criterion has to line up.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Confirm that a trailing space is the cause

Before changing any data, check one record that should match but is not being counted.

#1 Best Overall
Sale
Microsoft Excel Laminated Two-Sided Keyboard Shortcut Guide - Windows Edition
  • Over 215 Microsoft Windows Excel Shortcuts
  • Two-Sided Durable Laminiated Sheet
  • Designed for Excel on a Windows Computer
  1. Find a row that you expect to be included in the count, and note the text you see in its cell. For example, you might expect Smith.
  2. In an empty cell, enter =LEN(A2), replacing A2 with the cell you are checking. LEN returns the number of characters. If the visible word is five letters long and LEN returns 6, the cell contains one extra character.
  3. To flag every cell in the column that has extra ordinary spaces, enter =LEN(A2)<>LEN(TRIM(A2)) in a helper column and fill it down. A value of TRUE means TRIM would remove something from that cell.
  4. Check the criterion as well. If you typed "Smith " with a trailing space inside the formula, or the criterion comes from a cell that contains a trailing space, the count will be low for a different reason: the criterion itself is incomplete or wrong.

If LEN shows an extra character but the TRIM check in step 3 returns FALSE, the extra character is probably not an ordinary space. The section on nonbreaking spaces covers that case.

Normalize the values in a helper column

Do not overwrite your source data first. Build a cleaned copy beside it, count the cleaned copy, and compare the totals before you replace anything.

Rank #2
Sale
Microsoft Surface Pro Keyboard with Pen Storage, Compatible with Copilot+ (11th Edition), Surface 9 and 8, Alcantara Material, Black
  • Instant Copilot. Unlock new possibilities with the dedicated Copilot key, which gives you instant access to experiences that can enhance your productivity¹.
  • Enhance your experience With the new microphone mute key and snipping key
  • Full keyboard experience. Features a full mechanical keyset, backlit keys, and a large trackpad for precise navigation and control. Optimal key spacing allows fast, fluid typing.
  • Slim and compact Performs like a traditional, full-size keyboard.
  • Clicks in place instantly Use in combination with the Surface Pro (11th Edition), Pro 9 and Pro 8* kickstand for a perfect laptop experience anywhere.

Microsoft Excel

  1. Leave column A unchanged. In cell C2, enter =TRIM(A2), then fill the formula down to the last row of data.
  2. Count the cleaned values with a criterion that refers to column C, for example =COUNTIF(C:C,"Smith").
  3. Compare that result with the original count, which may be too low, and with a manual count of the rows you expect to match.
  4. When the results agree, copy column C and use Paste Special > Values over column A, after saving a backup copy of the workbook.

Google Sheets

  1. In cell C2, enter =TRIM(A2), then fill the formula down.
  2. Count the cleaned column with =COUNTIF(C:C,"Smith"), or point the criterion at a cell that holds the intended text.
  3. Compare the totals, then, if they agree, copy column C and paste it as values over column A. Keep a copy of the sheet or use Version history if you need to recover the original.

Google’s TRIM documentation says the function removes leading and trailing spaces and reduces repeated spaces inside the text to one. Google also notes that Sheets trims typed input by default, but spaces still matter inside formulas and validation rules. A criterion typed into a formula, or a value that arrived from another system, can therefore still carry a space.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Two points apply in both products. First, TRIM also collapses internal double spaces, so Mary Jane becomes Mary Jane. If your criterion expects the double space, the count will change for that reason. Second, the COUNTIF formula must point at the cleaned column. If it still refers to column A, the count will not change.

Rank #3
Pixiecube Excel Cheat Sheet Desk Pad | Excel Shortcut Keys Mouse Pad | Extended Large XL Gaming Mousepad | PC Office Spreadsheet Keyboard Mat | Non-Slip Stitched Edge
  • EXCEL SHORTCUTS. ZERO SEARCHING. – Our bestselling reference mat puts an extensive collection of commonly used commands, formulas and helpful tricks directly beneath your fingertips so you can find answers fast, work smarter and stay in the flow.
  • YOUR DESK. SMARTER. – Clearly organized sections for navigation, selection, formatting, data and functions make it easy to find the right Excel command exactly when you need it.
  • LEARN, WORK & RESET – Built-in desk-exercise diagrams give you 10 quick ways to stretch, recharge and return to work feeling sharper.
  • ROOM TO WORK & CREATE – The extended 31.5 x 11.8-inch Pixiecube desk mat fits a laptop or keyboard and mouse, while the soft 2 mm surface adds comfort and protects your desktop.
  • BUILT FOR REAL-WORLD WORKDAYS – A rugged stitched edge helps prevent fraying, and the water-resistant, stain-resistant surface protects against scratches, spills and everyday wear—because smarter desks should work harder.

When TRIM does not fix the count

TRIM handles ordinary spaces. It does not handle every space-like character, and this is where many people stop too early. Google’s TRIM help page states: “Whitespace or non-breaking spaces will not be trimmed.” Microsoft’s TRIM page states that, in Excel, “By itself, the TRIM function does not remove this nonbreaking space character.” A nonbreaking space (character code 160) often comes from web pages, PDFs, and some accounting or CRM exports. It looks like an ordinary space but is a different character.

Confirm a nonbreaking space or other hidden character

  • LEN returns a higher number than the visible text suggests.
  • The TRIM test in the earlier step returns FALSE, even though the text looks as if it has extra characters.
  • Removing ordinary spaces with TRIM changes nothing in the COUNTIF result.

Replace the characters and test again

  1. In Excel, enter =TRIM(SUBSTITUTE(A2,CHAR(160)," ")) in a helper cell. SUBSTITUTE converts the nonbreaking space to an ordinary space, and TRIM then removes the leading, trailing, and repeated spaces.
  2. For other nonprinting characters, Microsoft suggests trying CLEAN. A combined formula is =TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))).
  3. In Google Sheets, use the same SUBSTITUTE pattern, then confirm with LEN that the character count matches the visible text before you rely on it. Character handling can vary by application, so test on a few rows first.
  4. Run the COUNTIF against the new helper column and compare the result with your expected count.

Wildcards can make the count look right for the wrong reason

A criterion such as "*Smith*" matches any cell that contains the word, so it can also match Smithson or Smith Ltd. The same applies to "Smith*", which would also count Smith with a trailing space. A wildcard can raise the total and hide the data-quality problem you are trying to find. Use wildcards only when partial matching is the intended result. For cleanup, compare exact matches on normalized values.

Rank #4
Sale
Incase Wired Keyboard 600 – Designed by Microsoft – Spill Resistant, Quiet Touch Keys, Plug and Play, 4 Hotkeys, Windows Start Key – Black
  • Efficient Media Controls: The Wired Keyboard 600, designed by Microsoft, features a Media Center with four hot keys for easy control of play/pause, volume up, volume down, and mute functions.
  • Quiet and Responsive Keys: Enjoy a comfortable typing experience with quiet, thin-profile keys that are both responsive and efficient.
  • Convenient Shortcuts: Quickly access common tasks with dedicated shortcut keys, including a calculator hot key and a Windows start screen key.
  • Spill-Resistant Design: Work confidently with a spill-resistant design that protects your keyboard from accidental messes.
  • Plug-and-Play Simplicity: No software needed—just connect the keyboard to your PC and start using it right away, with a full number pad for efficient data entry.

Which problem matches which fix

Symptom Likely cause Fix Notes
Visible text matches, LEN is one higher, TRIM test shows TRUE Ordinary trailing or leading space =TRIM(A2) in a helper column TRIM also collapses repeated internal spaces
LEN is higher, TRIM test shows FALSE Nonbreaking space (character 160) =TRIM(SUBSTITUTE(A2,CHAR(160)," ")) TRIM alone does not remove it, per Google and Microsoft documentation
LEN is higher, no visible character Other nonprinting character CLEAN in Excel, combined with TRIM and SUBSTITUTE Test on a copy first; Google’s TRIM page does not cover these characters
Count is higher than expected Wildcard criterion such as "*text*" Use an exact criterion unless partial matching is intended Substring matches can include unrelated rows
Count is low or zero Criterion contains a trailing space Remove the space from the criterion or the cell it references The criterion is part of the comparison

Other causes to rule out

  • The range does not cover all the data. A fixed range such as A2:A100 will not count rows added below row 100. Whole-column references such as A:A avoid this, though they may include headers.
  • Numbers and text are stored differently. A numeric value and a text value that look the same may not match the same criterion.
  • The cell only appears blank. A cell containing a space or nonprinting character is not empty, so a check for blank cells will behave differently from COUNTIF on text.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Version and scope notes

Microsoft’s COUNTIF documentation lists Microsoft 365, Excel 2024, Excel 2021, and earlier specified versions. Its TRIM documentation lists Microsoft 365, Excel 2024, 2021, 2019, and 2016. Google’s Sheets help pages were reviewed in early October 2026, and the behavior described here comes from those pages. A Google Sheets community discussion from October 2020 includes a user asking why replacing a cell with identical-looking text changed the results. It shows that the symptom is familiar, but it does not establish the cause in any particular sheet.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The steps above are based on vendor documentation and have not been run against any particular workbook. Confirm each result on your own data, and keep a backup before replacing source values.

Best Value
SYNERLOGIC Microsoft Word/Excel (for Windows) Reference Guide Keyboard Shortcut Sticker, Laminated, No-Residue Vinyl (White/Small)
  • 💻 ✔️ EVERY ESSENTIAL SHORTCUT - With the SYNERLOGIC Reference Keyboard Shortcut Sticker, you have the most important shortcuts conveniently placed right in front of you. Easily learn new shortcuts and always be able to quickly lookup commands without the need to “Google” it.
  • 💻✔️ Work FASTER and SMARTER - Quick tips at your fingertips! This tool makes it easy to learn how to use your computer much faster and makes your workflow increase exponentially. It’s perfect for any age or skill level, students or seniors, at home, or in the office.
  • 💻 ✔️ New adhesive – stronger hold. It may leave a light residue when removed, but this wipes off easily with a soft cloth and warm, soapy water. Fewer air bubbles – for the smoothest finish, don’t peel off the entire backing at once. Instead, fold back a small section, line it up, and press gradually as you peel more. The “peel-and-stick-all-at-once” method only works for thin decals, not for stickers like ours.
  • 💻 ✔️ Compatible and fits any brand laptop or desktop running Windows 10 or 11 Operating System.
  • 💻 ✔️ Original Design and Production by Synerlogic Electronics, San Diego, CA, Boca Raton, FL and Bay City, MI, United States 2020. All rights reserved, any commercial reproduction without permission is punishable by all applicable laws.

The Bottom Line

A trailing space is a common, documented reason for COUNTIF to undercount text that looks identical. Measure the cells with LEN, normalize them in a helper column with TRIM, and use SUBSTITUTE with CHAR(160) and CLEAN when TRIM alone does not change the result. Keep wildcards for deliberate partial matches, and confirm each step against a copy of your data.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.