Ravindra BagaleCourses & study guides Track your progress
Easy challenge

Easy: Leading spaces break COUNTIF

Some SKUs have leading spaces. COUNTIF(A5:A10,"P-200") returns 0 in the broken file even though two rows look like P-200. TRIM the codes.

About this data: Made-up SKUs for Ananya Reddy (Hyderabad) and Luca Bianchi (Milan). Practice only.

Why → What → How

Why: Copy-paste from portals pads IDs. Exact-match functions then miss rows.

What: Six stock rows; three SKUs padded. COUNTIF for P-200 → 0 broken, → 2 fixed (LibreOffice verified).

How:

  1. Open sheet Stock.
  2. Find rows where SKU looks right but starts with spaces.
  3. TRIM or retype codes.
  4. Confirm COUNTIF → 2.

Ravindra Bagale’s Tip

LEN(A5) vs LEN(TRIM(A5)) exposes hidden spaces fast.

Bugs explained (with mini examples)

Bug 1 — Leading spaces

" P-200" ≠ "P-200". COUNTIF exact match fails. Fix: =TRIM(A5) helper column, then paste values.

Bug 2 — Eyes lie

The cell looks identical in a narrow column — widen or use LEN.

Practice tasks (12)

  1. Task 1. Widen column A — spot padded SKUs.
    Hint: Rows with leading spaces.
  2. Task 2. Compute LEN on a padded SKU.
    Hint: Longer than clean code.
  3. Task 3. COUNTIF for P-200 on broken sheet.
    Hint: 0.
  4. Task 4. TRIM the SKU column.
    Hint: Helper or in-place.
  5. Task 5. Re-run COUNTIF — expect 2.
    Hint: LibreOffice verified.
  6. Task 6. Does TRIM fix trailing spaces too?
    Hint: Yes.
  7. Task 7. Why not use *P-200* wildcards here?
    Hint: May over-match other codes.
  8. Task 8. Clean P-400 and P-200 Milan row.
    Hint: Same TRIM.
  9. Task 9. Interview line for this bug.
    Hint: “COUNTIF missed rows because of leading spaces.”
  10. Task 10. Use CLEAN+TRIM if non-printables appear.
    Hint: Paste from web.
  11. Task 11. Data → Text to Columns can also strip.
    Hint: Optional.
  12. Task 12. Bonus: add data validation length = 5 for SKUs.
    Hint: P-100 shape.

In-browser formula check

Type the fixed formula you would put in the named cell. We check formula text — open the workbook for full recalculation.

Step-by-step solution (try first)
  1. TRIM all SKUs.
  2. COUNTIF → 2. LibreOffice: broken 0, fixed 2.

Quick quiz

Q1. COUNTIF with leading spaces on the criteria key:
Q2. TRIM(" P-200") returns:
Q3. Best quick check for hidden spaces:
Q4. Fixed COUNTIF result in this file:
Q5. Leading spaces often come from: