Sub topic 5
Comparing text: quotes and case
This chapter covers how IF tests text values. It assumes you can write a basic =IF(test, value_if_true, value_if_false) formula and use comparison operators such as = and <>. It also assumes you can copy a formula down a column. The chapter explains why text needs quotes, how case is treated, and which hidden differences make two visible matches fail. It also covers partial matches and a few related functions.
Why text needs quotes
Excel reads anything inside a formula that is not a number, a cell reference, a function name, or an operator as a name. If you write Yes without quotes, Excel looks for a defined name called Yes. It does not find one and returns #NAME?.
Quotes mark a text string: a literal sequence of characters that Excel treats as data.
=IF(A2="Yes","Approved","Pending")This formula compares the contents of A2 with the string Yes. If they match, it returns Approved. Otherwise it returns Pending. Both results are also strings, so they are quoted too.
Quote rules to keep in mind:
- Use straight double quotes (
"). Curly quotes pasted from a word processor or web page cause a formula error. - Numbers are not quoted when you want a number.
=IF(A2=5,"Match","No")tests for the number 5. - Cell references are never quoted.
"A2"is the literal text A2, not the contents of cell A2. - A pair of quotes with nothing between them,
"", is an empty string: text of zero length.
Case does not matter
The = operator in Excel ignores upper and lower case. All of these return Approved when A2 holds yes, YES, or Yes.
=IF(A2="Yes","Approved","Pending")
=IF(A2="YES","Approved","Pending")
=IF(A2="yes","Approved","Pending")This is convenient for user-entered data, where people type the same word in many ways. The same rule applies to <>, so A2<>"yes" is false when A2 holds YES.
The comparison operators <, >, <=, and >= also work on text. Excel compares strings alphabetically, ignoring case. ="apple"<"banana" is TRUE. This is rarely useful in IF formulas, but it explains why a formula like =IF(A2>"M","Late alphabet","Early alphabet") runs without error.
Mixed types do not compare as equal
Text and numbers are different data types. The text "5" is not equal to the number 5.
| Formula | Result |
|---|---|
| `=5="5"` | FALSE |
| `=5=5` | TRUE |
| `="a"="A"` | TRUE |
| `="a"<>"A"` | FALSE |
This matters when numbers arrive as text, for example from a CSV import or a leading apostrophe. A cell showing 100 that is left-aligned and has a green triangle is probably text. A test like =IF(B2>=60,"Pass","Fail") can then give the wrong answer. Excel ranks text above numbers in comparisons, so "100">=60 returns TRUE regardless of the digits, and so would "5">=60.
You can convert text-numbers with VALUE:
=IF(VALUE(B2)>=60,"Pass","Fail")VALUE returns #VALUE! if the cell holds text that is not a number. Fixing the source data, for instance with Text to Columns, is usually better than patching every formula.
Case-sensitive comparison with EXACT
When case matters, such as product codes where ab12 and AB12 are different items, use EXACT. It returns TRUE only if two strings match character for character, including case.
=IF(EXACT(A2,"AB12"),"Correct code","Wrong code")EXACT takes two arguments and returns TRUE or FALSE, so it fits directly in the logical test position.
flowchart TD
A[Compare two text values] --> B{Does case matter?}
B -->|No| C["Use = or <>"]
B -->|Yes| D["Use EXACT(a, b)"]
C --> E[Wrap in IF as the logical test]
D --> EThe diagram above is shown as a plain code listing for reference. The version below is the one rendered in the chapter.
Invisible differences that break matches
A formula can look right and still return the wrong branch because the two strings are not identical. Common causes:
- Trailing or leading spaces.
"Yes "is not equal to"Yes". This is the most common cause. - Extra internal spaces.
"New York"with two spaces does not match"New York". - Non-breaking spaces. Text copied from web pages may contain character 160, which looks like a space but is not removed by
TRIM. - Different but similar characters. Straight and curly apostrophes, or a hyphen and an en dash, look alike but are different characters.
TRIM removes leading and trailing spaces and reduces runs of internal spaces to one. Use it inside the test:
=IF(TRIM(A2)="Yes","Approved","Pending")To also remove non-breaking spaces, replace character 160 first:
=IF(TRIM(SUBSTITUTE(A2,CHAR(160)," "))="Yes","Approved","Pending")A quick diagnostic is =LEN(A2). If a cell that shows Yes has a length of 4, there is a hidden character.
Testing for part of a string
The = operator requires a full match. To test whether a cell contains a word, use SEARCH or FIND inside ISNUMBER.
SEARCH(find_text, within_text) returns the position of the first match, or #VALUE! if there is none. It ignores case. FIND does the same but is case-sensitive. Wrapping the result in ISNUMBER turns it into TRUE or FALSE:
=IF(ISNUMBER(SEARCH("urgent",A2)),"Escalate","Normal")This returns Escalate for Urgent: server down, URGENT, and not urgent. The last one is a reminder that a substring test knows nothing about meaning.
SEARCH also supports wildcards: ? stands for one character and * for any run of characters. If you need to search for a literal ? or *, put a tilde in front of it (~?).
Wildcards do not work with the = operator. =IF(A2="Yes*",...) matches only text that literally ends in an asterisk. Wildcards do work in COUNTIF, SUMIF, and IFS-style lookups that use those functions, which you will meet in other contexts.
For prefix or suffix tests, LEFT and RIGHT are exact and easy to read:
=IF(LEFT(A2,3)="INV","Invoice","Other")Matching several accepted values
When more than one text value should produce the same result, combine tests with OR. Each comparison needs its own full test.
=IF(OR(D2="Gold",D2="Platinum"),"Priority","Standard")A frequent error is writing D2="Gold" or "Platinum" or OR(D2="Gold","Platinum"). In the second form, "Platinum" is a non-empty text value that is not a logical value, and OR returns #VALUE! because text cannot be read as TRUE or FALSE. Repeat the cell reference each time.
For long lists, an array constant keeps the formula short:
=IF(ISNUMBER(MATCH(D2,{"Gold","Platinum","Diamond"},0)),"Priority","Standard")MATCH with match type 0 finds an exact match and also ignores case. It returns a position when found and #N/A when not, so ISNUMBER converts the outcome to TRUE or FALSE. If the list is long or changes often, store it in cells and point MATCH at the range instead of using a constant.
Blank cells and text tests
A blank cell is equal to an empty string, so =IF(A2="","Missing","Present") treats both a truly empty cell and a cell holding "" from a formula as blank. A cell containing only a space is not blank to this test. TRIM(A2)="" catches that case as well. Blank handling is covered in more detail in a later chapter.
One subtle point: a blank cell compared with 0 is also TRUE, because a blank is treated as zero in numeric comparison. =IF(A2=0,...) fires for an empty A2. Choose A2="" or ISBLANK(A2) when you mean empty.
Worked example
You have a support-ticket sheet.
| Row | A (Status) | B (Subject) | C (Result) |
|---|---|---|---|
| 2 | open | Printer jam | |
| 3 | Closed | URGENT: login fails | |
| 4 | Open | Invoice question |
The goal is to flag open tickets with the word "urgent" in the subject, and to tolerate sloppy status entries.
=IF(AND(TRIM(A2)="Open",ISNUMBER(SEARCH("urgent",B2))),"Escalate","Routine")How it evaluates for each row:
TRIM(A2)strips stray spaces. Comparing with"Open"ignores case, soopenandOpenboth match.SEARCH("urgent",B2)returns a position or an error.ISNUMBERturns that into TRUE or FALSE.ANDrequires both to be TRUE.- IF returns
EscalateorRoutine.
Row 2 is open but not urgent, so it returns Routine. Row 3 is urgent but closed, so it returns Routine. Row 4 is open without the word, so it returns Routine. If row 2's subject were Urgent: Printer jam, the result would be Escalate.
Troubleshooting checklist
| Symptom | Likely cause | Fix |
|---|---|---|
| `#NAME?` | Text without quotes | Wrap literals in straight double quotes |
| Formula refuses to enter, or text appears inside the result | Unbalanced or curly quotes | Retype quotes in the formula bar |
| Always returns the false branch | Hidden spaces or lookalike characters | Use `TRIM`, `SUBSTITUTE`, and check `LEN` |
| `"Yes"` fails to match case you wanted to distinguish | `=` ignores case | Use `EXACT` |
| Number test passes when it should fail | Numbers stored as text | Convert with `VALUE` or fix the import |
| Wildcard in `=` does not work | `=` does not support wildcards | Use `SEARCH` with `ISNUMBER` |
| `#VALUE!` from `OR` | Bare text passed as a condition | Write a full comparison per value |
Exercises
Exercise
Cell A2 contains approved. What does =IF(A2="APPROVED","Go","Stop") return, and which function would make the test case-sensitive?
Show answer
It returns Go, because the = operator ignores case. To make the test case-sensitive, use =IF(EXACT(A2,"APPROVED"),"Go","Stop"), which would return Stop here.
Exercise
A cell displays Yes but =IF(A2="Yes","OK","Check") returns Check. =LEN(A2) returns 4. Name the likely cause and give a formula that returns OK.
Show answer
The cell contains a hidden extra character, most likely a trailing space (length 4 instead of 3). Use =IF(TRIM(A2)="Yes","OK","Check"). If the extra character is a non-breaking space, use =IF(TRIM(SUBSTITUTE(A2,CHAR(160)," "))="Yes","OK","Check").
Exercise
Write a formula that returns Flagged when B2 contains the word "refund" anywhere in the text, in any case, and OK otherwise.
Show answer
=IF(ISNUMBER(SEARCH("refund",B2)),"Flagged","OK"). SEARCH is case-insensitive and returns a position when the word is found, and ISNUMBER converts that to TRUE.