Spreadsheet Practice

How to find cells with leading or trailing spaces in Excel

Formula (Excel and Google Sheets)

=OR(LEFT(A2)=" ",RIGHT(A2)=" ")

Check the first and last characters: =OR(LEFT(A2)=" ",RIGHT(A2)=" ") returns TRUE for cells that start or end with a space.

Result on the example
one result per row in B2, filled down to B7
Works in
Every current version
Google Sheets
Same formula

See it work

The formula is already in B2, filled down to B7. Change any input and the result updates, or edit the formula to experiment.

How it works

LEFT and RIGHT with no length return one character, the first and the last. If either is a space, the cell has a leading or trailing space, the kind that makes "Contoso " fail to match "Contoso" in a lookup.

=OR(LEFT(A2)=" ",RIGHT(A2)=" ")

ResultFALSE

  1. A2"Northwind"

    The value in A2.

  2. LEFT(A2)"N"

    LEFT: Characters from the start of text.

    • textA2"Northwind"
  3. LEFT(A2)=" "FALSE

    The = operator tests whether the two sides are equal.

  4. RIGHT(A2)"d"

    RIGHT: Characters from the end of text.

    • textA2"Northwind"
  5. RIGHT(A2)=" "FALSE

    The = operator tests whether the two sides are equal.

  6. OR(LEFT(A2)=" ",RIGHT(A2)=" ")FALSE

    OR: TRUE if any argument is true.

    • logical1LEFT(A2)=" "FALSE
    • logical2RIGHT(A2)=" "FALSE

In Google Sheets

The same formula works in Google Sheets: =OR(LEFT(A2)=" ",RIGHT(A2)=" "). We ran it in Google Sheets mode on the same data and it returns the same results.

Other ways to do it

Each one was run on the same example sheet.

FormulaResultWhen to use it
=A2<>TRIM(A2)FALSE, TRUE, TRUE…Different resultCatches any extra space, including doubled spaces inside the text. "Adatum Corp", with two spaces in the middle, is TRUE too.

Common mistakes

  • Non-breaking spaces (character 160) aren't " ", so these tests miss them. Add SUBSTITUTE(A2,CHAR(160)," ") inside.
  • An empty cell returns FALSE, which is what you want; a cell with only a space returns TRUE.

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.

Find cells with leading or trailing spaces

In B2, return TRUE when the customer name starts or ends with a space, and fill down to B7.

Questions

How do I count the cells with extra spaces?

Use =SUMPRODUCT(--(A2:A7<>TRIM(A2:A7))).

How do I highlight them?

Use the test as a conditional formatting rule: =OR(LEFT(A2)=" ",RIGHT(A2)=" ").

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.