Explanation
This formula uses two named ranges : things , and results . If you are porting this formula directly, be sure to use named ranges with the same names (defined based on your data). If you don’t want to use named ranges, use absolute references instead.
The core of this formula is this snippet:
ISNUMBER(SEARCH(things,B5)
This is based on another formula ( explained in detail here ) that checks a cell for a single substring. If the cell contains the substring, the formula returns TRUE. If not, the formula returns FALSE.
Because we are giving the SEARCH function more than one thing to look for, in the named range things , it will give us more the one result, in an array that looks like this:
{#VALUE!;9;#VALUE!;#VALUE!}
Numbers represent matches in things , errors represent items that were not found.
To simplify the array, we use the ISNUMBER function to convert all items in the array to either TRUE or FALSE. Any valid number becomes TRUE, and any error (i.e. a thing not found) becomes FALSE. The result is an array like this:
{FALSE;TRUE;FALSE;FALSE}
which goes into the MATCH function as the lookup_array argument, with a lookup_value of TRUE:
MATCH(TRUE,{FALSE;TRUE;FALSE;FALSE},0) // returns 2
MATCH then returns the position of first TRUE found, 2 in this case.
Finally, we use the INDEX function to retrieve a result from the named range results at that same position:
=INDEX(results,2) // returns "found red"
You can customize the results range with whatever values make sense in your use case.
Preventing false matches
One problem with this approach with the ISNUMBER + SEARCH approach is you may get false matches from partial matches inside longer words. For example, if you try to match “dr” you may also find “Andrea”, “drank”, “drip”, etc. since “dr” appears inside these words. This happens because SEARCH automatically does a “contains-type” match.
For a quick fix, you can wrap search words in space characters (i.e. " dr “, or “dr “) to avoid finding “dr” in another word. But this will fail if “dr” appears first or last in a cell.
If you need a more robust solution, one option is to normalize the text first in a helper column , and add a leading and trailing space. Then use the formula on this page on the text in the helper column, instead of the original text.
Explanation
In this example, the goal is to map the numbers 1-6 to the arbitrary values seen in the table below. For example:
- If the input is 1, the output should be 10
- If the input is 2, the output should be 81
- If the input is 3, the output should be 17
- If the input is 4, the output should be 23
- And so on…
| Input | Output |
|---|---|
| 1 | 10 |
| 2 | 81 |
| 3 | 17 |
| 4 | 23 |
| 5 | 13 |
| 6 | 31 |
Although we could solve this problem with a complicated nested IF formula , a better option is to put the table on the worksheet and perform a lookup operation. The VLOOKUP function provides an easy way to do this. In the example shown, the formula in F7 is:
=VLOOKUP(F6,B5:C10,2,0)
- lookup_value - the value in cell F6 (4)
- table_array - the range B5:C10
- col_index_num - 2, to specify the second column
- range_lookup - zero, to force an exact match
Although in this case, we are mapping numeric inputs to numeric outputs, the same basic approach will readily handle text values for both inputs and outputs. A good example is converting test scores to grades .
Alternative with CHOOSE
If you have a limited number of inputs, and if the inputs are numbers starting with 1, you can also use the CHOOSE function . For the example shown the equivalent formula based on CHOOSE is:
=CHOOSE(F6,10,81,17,23,13,31)
The choose function is unwieldy for large amounts of data but for smaller data sets that map to a 1-based index, it has the advantage of being a simple “all in one” solution.