Explanation
In this example, the goal is to extract a set of records that match a partial text string . To keep things simple, we are only matching one field in the data, the last name (“Last”). The core operation of this formula comes from the FILTER function (new in Excel 365 ) which extracts matching data from a range based on a logical filter:
=FILTER(data,logic)
The challenge in this example is to construct the logic needed to match records based on a partial match. The FILTER function does not support wildcards , so we need to use a different approach. In this case, we are using the SEARCH function together with the ISNUMBER function like this:
ISNUMBER(SEARCH(H4,Table1[Last])) // partial match logic
Here, the SEARCH function is configured to look for text entered in cell H4, inside the “Last” column in the table. If SEARCH finds a result, it returns the position of that result in the text. For example:
=SEARCH("cat","The cat in the hat") // returns 5
If SEARCH doesn’t find anything, it returns the #VALUE! error:
=SEARCH("dog","The cat in the hat") // returns #VALUE!
In other words, if SEARCH returns a number, we have a match. If not, we don’t have a match. To convert this result into a simple TRUE/FALSE value, we wrap the SEARCH function inside the ISNUMBER function . ISNUMBER will return TRUE only when SEARCH has returned a number:
=ISNUMBER(SEARCH("cat","The cat in the hat")) // returns TRUE
=ISNUMBER(SEARCH("dog","The cat in the hat")) // returns FALSE
Notice we are not using a wildcard like ("*") to get a partial match, but the SEARCH + ISNUMBER combo behaves like a partial match nonetheless. If the search string is found anywhere in the text, SEARCH will return a number and ISNUMBER will return TRUE.
Getting back to the example shown, we are using SEARCH and ISNUMBER like this:
ISNUMBER(SEARCH(H4,Table1[Last])) // partial match logic
Because the table contains 100 rows, the snippet above will generate 100 TRUE/FALSE results, one for each cell in the Last column. This is exactly what we need for the FILTER function – a TRUE or FALSE value for each row in the data. When the snippet above is embedded in FILTER as the include argument, FILTER returns the matching rows:
=FILTER(Table1,ISNUMBER(SEARCH(H4,Table1[Last]))
We now have a working formula but need to tidy up a few things. First, if the FILTER function does not return any match, it will return a #CALC! error. To provide a more friendly message, we add a text message for the if_empty argument :
=FILTER(Table1,ISNUMBER(SEARCH(H4,Table1[Last])),"No results")
Now if the search text isn’t found, FILTER will return “No results”.
Finally, we need to handle the case of the search text in H4 being empty. Somewhat oddly, the SEARCH function will return the number 1 if the search text is an empty string:
=ISNUMBER(SEARCH("","The cat in the hat")) // returns 1
This will cause FILTER to return all results if cell H4 is empty, since ISNUMBER will happily return TRUE for number 1. To prevent this behavior, we tack on a bit of logic to the original logical expression:
ISNUMBER(SEARCH(H4,Table1[Last]))*(H4<>"")
The expression H4<>"" returns TRUE only when H4 is not empty, and FALSE when H4 is empty. When we multiply the results of this expression by the original SEARCH + ISNUMBER expression, it will “cancel out” all TRUE results when H4 is empty. This is a form of Boolean logic . The math operation of multiplication (*) works like AND logic in array formulas .
Partial match with INDEX and MATCH
The FILTER function is available only in Excel 365. In older versions of Excel, it is possible to set up a partial match formula to return more than one match, but it is more complicated. This formula shows one approach based on INDEX and MATCH .
Explanation
The new dynamic array formulas in Excel 365 make it much easier to solve certain tricky problems with formulas.
In this example, the goal is to generate a list of random 6-character codes. The randomness is handled by the RANDARRAY function , a new function in Excel 365. RANDARRAY returns 6 random numbers to INDEX, which then retrieves 6 random values from the named range chars. The results from INDEX are then concatenated together with the TEXTJOIN function.
In the example shown, the formula in D5 is:
=TEXTJOIN("",1,INDEX(chars,RANDARRAY(6,1,1,26,TRUE)))
Working from the inside out, the RANDARRAY function is used to generate an array containing six random numbers between 1-26:
RANDARRAY(6,1,1,26,TRUE) // return array like {14;5;21;7;25;3}
Note the array returned will vary with each instance of the RANDARRAY function. Also, because RANDARRAY is a volatile function , it will recalculate with each worksheet change.
This array of random numbers is returned directly to the INDEX function as the rows argument:
INDEX(chars,{14;5;21;7;25;3})
Because we are asking INDEX for 6 rows, we get back 6 results in an array like this:
{"N","E","U","G","Y","C"}
This array is returned to the TEXTJOIN function as the text1 argument:
=TEXTJOIN("",1,{"N","E","U","G","Y","C"}) // returns "NEUGYC"
TEXTJOIN is set to use an empty string as the delimiter and to ignore empty values. With this configuration, TEXTJOIN simply concatenates all values together and returns a 6-character text string like “NEUGYC”.
Count chars programmatically
Instead of hardcoding the size of chars directly into the RANDARRAY function, you can use the COUNTA function to count the elements in the array and return that count to RANDARRAY:
RANDARRAY(6,1,1,COUNTA(chars),TRUE)
This assumes chars does not contain any empty cells.
Generate chars programmatically
Since the letters A-Z have underlying numeric code values, it is possible to generate the array of characters used to assemble text strings programmatically, instead of using a range . This can be done with the CHAR function and the SEQUENCE function .
To generate an array with all uppercase letters A-Z, which map to ASCII 65-90:
=CHAR(SEQUENCE(26,1,65,1)) // returns {"A","B","C",...}
To generate lowercase letters a-z, which correspond to ASCII 97-122:
=CHAR(SEQUENCE(26,1,97,1)) // returns {"a","b","c",...}
This code can be dropped into the original formula to replace chars like this:
=TEXTJOIN("",1,INDEX(CHAR(SEQUENCE(26,1,65,1)),RANDARRAY(6,1,1,26,TRUE)))
Other characters
The characters in the named range chars can be anything you like. If you add more than 26 characters (or fewer) adjust the number 26 as appropriate, or use COUNTA as explained above.
Without Excel 365
It is possible to generate random text strings without Excel 365, but the formula is more tedious and redundant. Since we don’t have a good way to get 6 random numbers all at once, we use the RANDBETWEEN function to get one random value at a time:
=INDEX(chars,RANDBETWEEN(1,26))&
INDEX(chars,RANDBETWEEN(1,26))&
INDEX(chars,RANDBETWEEN(1,26))&
INDEX(chars,RANDBETWEEN(1,26))&
INDEX(chars,RANDBETWEEN(1,26))&
INDEX(chars,RANDBETWEEN(1,26))
This formula uses the INDEX function to retrieve one random value at a time from the named range chars , and the 6 results are concatenated together into a single text string. Line breaks added for readability .
It is also possible to generate values A-Z directly with CHAR and RANDBETWEEN like this:
=CHAR(RANDBETWEEN(65,90))&
CHAR(RANDBETWEEN(65,90))&
CHAR(RANDBETWEEN(65,90))&
CHAR(RANDBETWEEN(65,90))&
CHAR(RANDBETWEEN(65,90))&
CHAR(RANDBETWEEN(65,90))
In this version, RANDBETWEEN is returning a value between 65 and 90 (inclusive) that corresponds to the ASCII value for the letters A-Z (uppercase). The CHAR function translates the numeric value to a letter. As above, all results are concatenated together in a single text string.