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 toB7 - 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
A2"Northwind"The value in A2.
LEFT(A2)"N"LEFT: Characters from the start of text.- text
A2"Northwind"
- text
LEFT(A2)=" "FALSEThe = operator tests whether the two sides are equal.
RIGHT(A2)"d"RIGHT: Characters from the end of text.- text
A2"Northwind"
- text
RIGHT(A2)=" "FALSEThe = operator tests whether the two sides are equal.
OR(LEFT(A2)=" ",RIGHT(A2)=" ")FALSEOR: TRUE if any argument is true.- logical1
LEFT(A2)=" "FALSE - logical2
RIGHT(A2)=" "FALSE
- logical1
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.
| Formula | Result | When to use it |
|---|---|---|
| =A2<>TRIM(A2) | FALSE, TRUE, TRUE…Different result | Catches 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
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.