How to Compare Two Strings in Excel
In this article
To compare two strings in Excel, use a simple formula like =A1=B1, which returns TRUE if the strings match exactly and FALSE otherwise. For case-insensitive comparison, that's all you need. For case-sensitive comparison, use =EXACT(A1,B1). You can also use IF to return custom text like "Match" or "No match", or conditional formatting to highlight differences visually. This guide walks through each method step by step, plus common pitfalls and troubleshooting tips.
What You'll Need
- Microsoft Excel (any modern version: Excel for Microsoft 365, Excel 2019, 2021, or Excel for Mac). The steps below work in all of them; menu labels may differ slightly by version and platform.
- Two strings to compare, either in separate cells or typed directly into a formula.
- Basic familiarity with entering formulas (starting with =).
Step-by-Step
- Enter your strings. Put the first string in cell A1 and the second in B1 (or use any cells you prefer). For example, A1: Apple, B1: apple.
- Use the equality operator for a case-insensitive check. In cell C1, type
=A1=B1and press Enter. Excel returns TRUE if the strings match ignoring case, FALSE if they differ. In the example, it returns TRUE because Excel treats "Apple" and "apple" as equal. - Use EXACT for a case-sensitive check. In cell D1, type
=EXACT(A1,B1)and press Enter. This returns TRUE only if the strings match exactly, including uppercase and lowercase. In the example, it returns FALSE. - Wrap in IF to get friendly labels. Instead of TRUE/FALSE, use
=IF(A1=B1,"Match","No match")or=IF(EXACT(A1,B1),"Match","No match"). This makes results easier to read, especially in reports. - Compare strings directly in a formula. If you don't want to reference cells, type the strings inside the formula:
="Apple"="apple"returns TRUE;=EXACT("Apple","apple")returns FALSE. Enclose text in double quotes. - Apply conditional formatting to highlight differences. Select the range you want to check (e.g., A1:B10). Go to Home > Conditional Formatting > New Rule > Use a formula to determine which cells to format. Enter a formula like
=$A1<>$B1(for case-insensitive) or=NOT(EXACT($A1,$B1))(for case-sensitive). Click Format, choose a fill color, and click OK. Cells where the strings differ will be highlighted. - Copy the formula down. If you have multiple rows, drag the fill handle from C1 down to apply the formula to other rows. Relative references adjust automatically.
- Check for leading or trailing spaces. If strings look identical but the formula returns FALSE, there may be hidden spaces. Use
=TRIM(A1)=TRIM(B1)to compare after removing extra spaces. For case-sensitive with trimming, use=EXACT(TRIM(A1),TRIM(B1)).
Common Mistakes
- Forgetting that = is case-insensitive. Many users expect "Apple" and "apple" to be different, but = treats them as equal. Use EXACT when case matters.
- Including spaces in the comparison. A trailing space in one cell but not the other will cause a mismatch. TRIM helps, but it also removes multiple internal spaces, which may or may not be desired.
- Comparing numbers stored as text. If one cell contains the number 123 and another contains the text "123", = may return TRUE or FALSE depending on formatting. Use VALUE or TEXT to align types if needed.
- Using EXACT with non-text values. EXACT works on text; if you pass numbers, they are converted to text, which can lead to unexpected results (e.g., 1 vs 1.0).
- Not anchoring references in conditional formatting. When applying a formula to a range, use mixed references (like $A1) so the formula checks each row correctly.
Tips
- To compare two columns and count matches, use
=SUMPRODUCT(--(A1:A10=B1:B10))for case-insensitive, or=SUMPRODUCT(--EXACT(A1:A10,B1:B10))for case-sensitive. - Use
=LEN(A1)=LEN(B1)as a quick check before comparing; if lengths differ, strings can't be identical. - For a visual side-by-side, use the Equals operator in a helper column and filter for FALSE to see mismatches.
- If you need to compare strings across worksheets, reference them like
=Sheet1!A1=Sheet2!A1. - Remember that Excel's = operator ignores case but does not ignore spaces; EXACT ignores nothing.
Troubleshooting
- Formula returns FALSE but strings look identical. Check for leading/trailing spaces with
=LEN(A1)and=LEN(B1). Use TRIM or CLEAN to remove non-printing characters. - Formula returns #VALUE! error. This can happen if one of the cells contains an error value (like #N/A). Use IFERROR to handle it:
=IFERROR(A1=B1,"Error"). - Conditional formatting not highlighting differences. Ensure the formula uses relative references correctly and that the range selected matches the formula's starting cell. Also check that the rule is enabled and not conflicting with other rules.
- EXACT returns TRUE for numbers that look different. For example, 1 and 1.0 are stored the same; EXACT converts them to text "1" and "1", so they match. If you need to distinguish, compare the underlying values with =.
- Comparison ignores case when you want it to be case-sensitive. Switch from = to EXACT.
Frequently Asked Questions
How do I compare two strings in Excel ignoring case?
Use the equality operator: =A1=B1. This returns TRUE if the strings are the same regardless of uppercase or lowercase.
How do I compare two strings in Excel with case sensitivity?
Use the EXACT function: =EXACT(A1,B1). It returns TRUE only if the strings match exactly, including case.
Can I compare more than two strings at once?
Yes, you can chain comparisons: =AND(A1=B1,B1=C1) returns TRUE if all three are equal (case-insensitive). For case-sensitive, use =AND(EXACT(A1,B1),EXACT(B1,C1)).
Why does my comparison return FALSE when the strings look the same?
Likely due to hidden spaces or non-printing characters. Use =TRIM(A1)=TRIM(B1) or =CLEAN(A1)=CLEAN(B1) to remove them before comparing.
How can I highlight cells where two strings differ?
Use conditional formatting with a formula like =$A1<>$B1 (case-insensitive) or =NOT(EXACT($A1,$B1)) (case-sensitive). Apply it to the range you want to check.
Does Excel have a built-in tool to compare two columns?
Excel doesn't have a dedicated "compare columns" button, but you can use formulas in a helper column or conditional formatting to achieve the same result quickly.