by Svetlana Cheusheva, updated on
The tutorial shows how to compare text strings in Excel for case-insensitive and exact match. You will learn a number of formulas to compare two cells by their values, string length, or the number of occurrences of a specific character, as well as how to compare multiple cells.
When using Excel for data analysis, accuracy is the most vital concern. Incorrect information leads to missed deadlines, misjudged trends, wrong decisions and lost revenues.
While Excel formulas are always perfectly true, their results may be wrong because some flawed data penetrated into the system. In this case, the only remedy is to check data for accuracy. It's no big deal to compare two cells manually, but it's next to impossible to spot the differences between hundreds and thousands of text strings.
This tutorial will teach you how to automate the tedious and error-prone task of cell comparison and what formulas are best to use in each particular case.
There are two different ways to compare strings in Excel depending on whether you seek case-sensitive or case-insensitive comparison.
To compare two cells in Excel ignoring case, use a simple formula like this:
=A1=B1
Where A1 and B1 are the cells you are comparing. The result of the formula are Boolean values TRUE and FALSE.
If you want to output your own texts for matches and differences, embed the above statement in the logical test of the IF function. For example:
=IF(A1=B1, "Equal", "Not equal")
As you see in the screenshot below, both formulas compare text strings, dates and numbers equally well:
In some situations, it may be important not only to compare text values of two cells, but also to compare the character case. Case-sensitive text comparison can be done using the Excel EXACT function:
Where text1 and text2 are the two cells you are comparing.
Assuming your strings are in cells A2 and B2, the formula goes as follows:
=EXACT(A2, B2)
As the result, you get TRUE for text strings match exactly including the case of each character, FALSE otherwise.
If you want the EXACT function to deliver some other results, embed it in an IF formula and type your own text for value_if_true and value_if_false arguments:
=IF(EXACT(A2 ,B2), "Exactly equal", "Not equal")
The following screenshot shows the results of the case-sensitive string comparison in Excel:
To compare more than 2 cells in a row, use the formulas discussed in the above examples in combination with the AND operator. The full details follow below.
Depending on how you want to display the results, utilize one of the following formulas:
=AND(A2=B2, A2=C2)
or
=IF(AND(A2=B2, A2=C2), "Equal", "Not equal")
The AND formula returns TRUE if all of the cells contain the same value, FALSE if any value is different. The IF formula outputs the labels that you type in it, "Equal" and "Not equal" in this example.
As demonstrated in the screenshot below, the formula works perfectly with any data types - text, dates and numeric values:
To compare multiple strings to each other to see if they match exactly, use the following formulas:
=AND(EXACT(A2,B2), EXACT(A2, C2))
Or
=IF(AND(EXACT(A2,B2), EXACT(A2, C2)),"Exactly equal", "Not equal")
Like in the previous example, the first formula delivers TRUE and FALSE values, whereas the second one displays your own texts for matches and differences:
The following examples show how you can verify that all cells in a given range contain the same text as in a sample cell.
If the character case does not really matter, you can use the following formula to compare cells to a sample:
In the logical test of the IF function, you compare two numbers:
Assuming the sample text is in C2 and the strings to compare are in the range A2:B6, the formula goes as follows:
=ROWS(A2:B6)*COLUMNS(A2:B6)=COUNTIF(A2:B6,C2)
To make the results more user-friendly, i.e. output something like "All match" and "Not all match" instead of TRUE and FALSE, use the IF function like we did in the previous examples:
=IF(ROWS(A2:B6)*COLUMNS(A2:B6)=COUNTIF(A2:B6,C2),"All match", "Not all match")
As shown the above screenshot, the formula perfectly copes with a range of text strings, but it can also be used to compare numbers and dates.
If the character case makes a difference, you can compare strings to the sample text using the following array formulas.
With the source range residing in A2:B6 and the sample text in C2, the formula takes the following shape:
=IF(ROWS(A2:B6)*COLUMNS(A2:B6)=SUM(--EXACT(C2, A2:B6)), "All match", "Not all match")
Unlike regular Excel formulas, array formulas are completed by pressing Ctrl + Shift + Enter. If entered correctly, Excel encloses the array formula in {curly braces}, as shown in the screenshot:
To identify if a certain value is present or absent in a range, please see Check if a value exists in a range.
Sometimes you may want to check if the text strings in each row contain an equal number of characters. The formula for this task is very simple. First, you get the string length of two cells using the LEN function, and then compare the numbers.
Supposing the strings to be compared are in cells A2 and B2, use either of the following formulas:
=LEN(A2)=LEN(B2)
Or
=IF(LEN(A2)=LEN(B2), "Equal", "Not equal")
As you already know, the first formula returns Boolean values TRUE or FALSE, whereas the second formula outputs your own results:
As demonstrated in the screenshot above, the formulas work for text strings as well as numbers.
Tip. If two seemingly equal strings return different lengths, most likely the problem is in leading or trailing spaces in one or both cells. In this case, remove extra spaces using the TRIM function. The detailed explanation and formula examples can be found here: How to trim spaces in Excel.
This is the last example in our Excel Compare Strings tutorial, and it shows a solution for a rather specific task. Supposing, you have 2 columns of text strings that contain a character important to you. Your goal is to check whether two cells in each row contain the same number of occurrences of a given character.
To make things clearer, consider the following example. Let's say, you have two lists of orders shipped (column B) and received (column C). Each row contains orders for a specific item, whose unique identifier is included in all order IDs and is listed in the same row in column A (please see the screenshot below). You want to make sure that each row contains an equal number of shipped and received items with that specific ID.
To solve this problem, write a formula with the following logic.
SUBSTITUTE(A1, character_to_count,"")
LEN(cell 1) - LEN(SUBSTITUTE(cell 1, character_to_count, ""))
and
LEN(cell 2) - LEN(SUBSTITUTE(cell 2, character_to_count, ""))
In our example, the unique identifier is in A2, and the strings to compare are in cells B2 and C2. So, the complete formula is as follows:
=LEN(B2)-LEN(SUBSTITUTE(B2,$A2,""))=LEN(C2)-LEN(SUBSTITUTE(C2,$A2,""))
The formula returns TRUE if cells B2 and C2 contain an equal number of occurrences of the character in A2, FALSE otherwise. To make the results more meaningful for your users, you can embed the formula in the IF function:
=IF(LEN(B2)-LEN(SUBSTITUTE(B2, $A2,""))=LEN(C2)-LEN(SUBSTITUTE(C2, $A2,"")), "Equal", "Not equal")
As you can see in the screenshot above, the formula works perfectly despite a couple of additional complications:
This is how you compare strings in Excel. To have a closer look at the formulas discussed in this tutorial, you are welcome to download our sample sheet below. I thank you for reading and hope to see you on our blog next week!
Excel - compare strings examples (.xlsx file)
Table of contents