LAMBDA contains one of many
Excel does not provide a dedicated "contains" function to test a cell for one or more text strings. However, you can use the LAMBDA function to create a custom function for this purpose. In the example below, the formula in cell C5 is:
This formula will return TRUE if a cell contains any one of the strings passed into it. Because it is built with the SEARCH function, it automatically performs partial matching and supports wildcards.
Excel does not provide a dedicated "contains" function, but you can create a custom function to test if a cell contains one or many strings with the LAMBDA function. LAMBDA functions do not require VBA, but are only available in Excel 365.
The first step in creating a custom LAMBDA function is to verify the logic needed with a Excel standard formula. This LAMBDA formula is based on a Excel formula created three functions: SUMPRODUCT, ISNUMBER, and SEARCH:
Read a detailed description here. Because the LAMBDA function is only available in the dynamic array version of Excel, which handles array formulas natively, we are using SUM instead of SUMPRODUCT (see note below), and renaming "things" to "strings" to make the formula arguments a bit more natural:
The screen below shows this formula in use with three strings "red", "blue", and "green":
This formula returns TRUE for any cell in column B that contains any one of the strings "red", "blue", or "green".
The next step is to convert this formula into a generic (unnamed) LAMBDA formula. We will need two input parameters, one for the text, and one for the strings to test. These need to appear as the first arguments in the LAMBDA formula. The final argument contains the calculation to perform, which is adapted from our standard Excel formula above. Here is the generic LAMBDA:
The screen below shows this formula in action, with the testing syntax needed to provide values for text and strings:
Note that results are the same as above. The next step in creating a custom LAMBDA is to name and define the formula with the Name Manager. In this case, we'll use the name "ContainsOneOfMany":
Finally, we update the worksheet to use the new custom function, and confirm that results are the same:
Although we are hard-coding the strings "red", "blue", and "green" as an array constant in this example for simplicity, the formula will work fine if we supply a range instead:
In addition, the formula will also work correctly if we supply only one string:
Note: Traditionally, SUMPRODUCT is often seen in array formulas, because it can handle arrays natively, without control + shift + enter. This makes the formula "more friendly" to most users. The SUM function can also be used in these cases, but the formula must then be entered with control + shift + enter. In Excel 365, the the SUM function will work in these cases without any special handling. Since LAMBDA is only available in Excel 365, this example uses SUM, since SUMPRODUCT provides no additional benefit.