Sub topic 5
Comparing text: quotes and case
This chapter assumes you can write a basic IF formula, use comparison operators, and copy a formula down a column. Here you apply those skills to text values, where two details cause most errors: quotation marks and upper or lower case.
Text needs quotes
Excel treats anything inside double quotes as literal text. Without quotes, Excel reads the word as a name, such as a defined name or a function, and usually returns #NAME?.
=IF(A2="Yes","Approved","Pending")Here A2="Yes" asks whether the cell contains the text Yes. The results "Approved" and "Pending" are also text, so they also need quotes.
The rule is simple:
- Numbers and cell references go without quotes:
B2>=60,$F$1. - Text goes inside straight double quotes:
"Yes","Gold". - An empty string is two quotes with nothing between them:
"".
A number inside quotes becomes text. "60" is not the number 60, so a comparison such as B2>="60" can give surprising results.
Case does not matter
The = comparison in Excel ignores upper and lower case. The formula above returns "Approved" for Yes, yes, YES, and yEs.
This is convenient when people type the same answer in different ways. It also means you cannot use = alone to tell Yes from YES. If you need a case-sensitive match, use the EXACT function inside the test:
=IF(EXACT(A2,"Yes"),"Approved","Pending")EXACT returns TRUE only when both texts match character for character, including case.
Matching must be complete
An = comparison on text checks the whole cell. It does not look for part of the text.
| Cell A2 contains | `=IF(A2="Yes",...)` test result |
|---|---|
| Yes | TRUE |
| yes | TRUE |
| Yes, approved | FALSE |
| Yes followed by a space | FALSE |
The last row is a frequent surprise. A trailing space is invisible, but it makes the text different. If data comes from another system, wrap the cell in TRIM to remove extra spaces:
=IF(TRIM(A2)="Yes","Approved","Pending")Not equal to
Use <> to test for text that is different from a value:
=IF(A2<>"Yes","Needs review","OK")Like =, this comparison ignores case. A cell containing yes counts as equal to "Yes", so the formula returns "OK".
How Excel evaluates a text test
Common text errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `#NAME?` | Text typed without quotes | Put the text in straight double quotes |
| Always returns the false result | Extra spaces or extra words in the cell | Use `TRIM`, or check the cell contents |
| Formula will not enter | Curly quotes or a missing closing quote | Retype the quotes |
| Case difference is not detected | `=` ignores case | Use `EXACT` |
Try it
Enter these values in A2:A5: Yes, yes, Yes (with a trailing space), and No. In B2, enter the formula below and copy it down.
=IF(A2="Yes","Approved","Pending")The results are Approved, Approved, Pending, and Pending. Now change B2 to use TRIM(A2) and copy it down again. The third row changes to Approved.
Exercise
A2 contains the text GOLD. What does =IF(A2="gold","Priority","Standard") return, and why? What would you change so that only an exact-case match of "gold" returns Priority?
Show answer
It returns "Priority", because the = comparison ignores upper and lower case. To require an exact-case match, use =IF(EXACT(A2,"gold"),"Priority","Standard"), which returns "Standard" for GOLD.
Exercise
The formula =IF(A2=Approved,"Done","Open") returns #NAME?. What is wrong, and what is the corrected formula?
Show answer
The word Approved is not in quotes, so Excel reads it as a name it cannot find. The corrected formula is =IF(A2="Approved","Done","Open").