Comparing two cells in Excel to see if one text string contains another is a common task. Excel provides several functions to accomplish this efficiently. Here are three effective methods:
1. Using the IF and FIND Functions
The FIND
function locates the starting position of one text string within another. When combined with the IF
function, you can determine if the text exists and return a TRUE or FALSE result.
=IF(ISNUMBER(FIND(B2,A2)),TRUE,FALSE)
Explanation:
FIND(B2,A2)
: This searches for the text in cellB2
within cellA2
. If found, it returns the starting position as a number. If not found, it returns an error value.ISNUMBER(...)
: This checks if the result ofFIND
is a number (meaning the text was found). It returnsTRUE
if a number andFALSE
if an error.IF(...)
: This returnsTRUE
ifISNUMBER
isTRUE
(text found) andFALSE
otherwise.
2. Using the MATCH Function (Excel 2016 and later)
The MATCH
function searches for a specific item within a range and returns its relative position. For comparing text within a single cell, use it with a wildcard.
=IF(ISNUMBER(MATCH("*"&B2&"*",A2,0)),TRUE,FALSE)
Explanation:
"*"&B2&"*"
: This creates a text string with asterisks (*) before and after the text in cellB2
. The asterisks act as wildcards, allowingMATCH
to find the text anywhere within cellA2
.MATCH(...,A2,0)
: This searches for the wildcard string within cellA2
with an exact match (match_type = 0). If found, it returns the position. If not found, it returns an error value.ISNUMBER(...)
: Checks if the result ofMATCH
is a number, indicating a match was found. returnsTRUE
for a number andFALSE
for an error.IF(...)
: ReturnsTRUE
if a match is found (ISNUMBER
is TRUE) andFALSE
otherwise.
3. Using the SEARCH Function with Wildcards
Similar to FIND
, the SEARCH
function is not case-sensitive. It can be combined with wildcards and the ISNUMBER
function for flexible text comparison.
=IF(ISNUMBER(SEARCH("*"&B2&"*",A2)),TRUE,FALSE)
Explanation:
This formula works similarly to the MATCH
example but uses SEARCH
instead. It’s less strict about matching the entire cell content since it allows wildcards and performs a case-insensitive search.
Conclusion
These three methods provide reliable ways to compare two cells in Excel containing text and determine if one string is contained within the other. Choose the formula that best suits your needs and Excel version. Remember to adjust cell references as needed for your specific data. Using these functions will enable you to efficiently analyze and manipulate text data within your spreadsheets.