Microsoft Excel is a powerful tool for data analysis, and often you need to compare text strings to see if one contains another. This “contains text” comparison is crucial for tasks like data validation, cleaning, and categorization. Instead of manually sifting through rows of data, Excel offers efficient formulas to automate this process.
This article will guide you through three distinct and effective methods to compare text in Excel using the “contains text” logic. Whether you’re a beginner or an experienced Excel user, these techniques will empower you to quickly identify cells containing specific text, saving you time and enhancing your data manipulation capabilities.
Let’s dive into each method with clear explanations and examples:
1. Leveraging the IF and FIND Functions for Text Comparison
This method combines the IF
and FIND
functions to perform a “contains text” check. The FIND
function is instrumental here as it locates the starting position of one text string within another. If the text is found, FIND
returns a number representing the starting position; otherwise, it returns an error. We then use ISNUMBER
to check if FIND
returned a number (meaning the text was found), and IF
to translate this into a TRUE/FALSE result.
=IF(ISNUMBER(FIND(B2,A2)),TRUE,FALSE)
- Breakdown:
FIND(B2,A2)
: This part searches for the text in cellB2
(the text you want to find) within the text in cellA2
(the text to search within).ISNUMBER(...)
: This function checks if the result of theFIND
function is a number. IfFIND
successfully locates the text, it returns a number, andISNUMBER
returnsTRUE
. IfFIND
doesn’t find the text, it returns an error, andISNUMBER
returnsFALSE
.IF(...,TRUE,FALSE)
: TheIF
function then uses theTRUE
orFALSE
result fromISNUMBER
. IfISNUMBER
isTRUE
(text found), theIF
function returnsTRUE
. IfISNUMBER
isFALSE
(text not found), theIF
function returnsFALSE
.
Example: If cell A2 contains “The quick brown fox jumps over the lazy dog Apple” and cell B2 contains “Apple”, the formula will return TRUE
. If B2 contains “Zebra”, it will return FALSE
.
2. Utilizing the MATCH Function (Excel 2016 and Later Versions)
For users with Excel 2016 or later versions, the MATCH
function offers another concise way to achieve “contains text” comparison. MATCH
is typically used to find the position of an item in a range, but with a slight adjustment, it can effectively check for text inclusion.
=IF(MATCH(B2,A2,0)>0,TRUE,FALSE)
- Explanation:
MATCH(B2,A2,0)
: This attempts to find an exact match for the text inB2
withinA2
. While it’s searching for an exact match, in the context of “contains text”, ifB2
is part ofA2
,MATCH
will find a “match” and return its relative position within the string (which will be greater than 0). The0
as the third argument specifies an exact match type, but here we are leveraging its behavior when the lookup value is part of the within-text value....>0
: This checks if the result ofMATCH
is greater than 0. IfMATCH
finds the text (even as part of a larger string), it returns a positive number (the position), making the conditionTRUE
. If the text isn’t found,MATCH
typically returns an error, but in this formula construction, it may return other non-positive values or errors which will be evaluated toFALSE
by theIF
condition.IF(...,TRUE,FALSE)
: Similar to the previous method,IF
translates the boolean result intoTRUE
orFALSE
.
Note: This method is available in Excel 2016 and subsequent versions. Ensure your Excel version is compatible.
3. Employing SUMPRODUCT with Wildcards for Flexible Text Matching
The SUMPRODUCT
function, combined with wildcards, provides a more versatile approach, especially when dealing with ranges of cells. Wildcards are special characters that allow for pattern matching in text strings. In Excel, the asterisk (*
) wildcard represents any sequence of characters.
=SUMPRODUCT(--(ISNUMBER(SEARCH(B2,A2))))>0
Alternatively, for older versions or slightly different behavior (depending on version and settings):
=SUMPRODUCT(--(A2:A5*B2))>0
*(Note: The second formula with `A2:A5B2might have limitations or require careful context due to potential implicit type conversions and is less robust than using
SEARCHand
ISNUMBER` for explicit text comparison.)**
Let’s break down the more robust SEARCH
and ISNUMBER
version:
SEARCH(B2,A2)
: Similar toFIND
,SEARCH
locates text within another text string. However,SEARCH
is not case-sensitive and allows for wildcards in the search string (though we are not using wildcards inB2
in this example, but searching for exact content ofB2
withinA2
). It returns the starting position if found, and an error if not.ISNUMBER(...)
: Checks ifSEARCH
returned a number (text found).--(ISNUMBER(SEARCH(B2,A2)))
: The double negative (--
) converts theTRUE
orFALSE
result fromISNUMBER
into 1 or 0, respectively. This is necessary forSUMPRODUCT
to work correctly.SUMPRODUCT(...)
: In this case,SUMPRODUCT
is used to sum up the results (1s and 0s). If the text inB2
is found withinA2
,SUMPRODUCT
will sum at least a1
, resulting in a sum greater than 0....>0
: Checks if the sum is greater than 0. If yes, it means the text was found at least once, and the formula returnsTRUE
; otherwise, it returnsFALSE
.
Example: Using the first SUMPRODUCT
formula with SEARCH
: If cell A2 contains “The quick brown fox jumps over the lazy dog Apple” and cell B2 contains “Apple”, the formula returns TRUE
. If B2 contains “Zebra”, it returns FALSE
.
Choosing the Right Method for Your Needs
- For simplicity and basic “contains text” checks, the
IF
andFIND
combination is often the easiest to understand and implement. - If you are using Excel 2016 or later and prefer a slightly more concise formula, the
MATCH
function method is a good option. - When dealing with ranges of cells or requiring case-insensitive search, the
SUMPRODUCT
withSEARCH
method offers more flexibility.
All three methods effectively address the need to compare text in Excel and determine if a cell “contains text”. Choose the formula that best suits your Excel version, complexity requirements, and personal preference. Start applying these formulas to streamline your Excel tasks and gain deeper insights from your data!