In cell A2
(see attached image from question), I've placed this formula:
=IF($E$2="","",IFERROR(INDEX(A$7:A$28,SMALL(IF(ISNUMBER(SEARCH($E$2,A$7:A$28)),ROW(A$7:A$28)-ROW(A$7)+1),ROWS(A$2:A2))),""))
then pressing CTRL+SHIFT+ENTER to enter the formula as an array formula. Finally, dragging this formula down to A5
, and then dragging A2:A5
over to the C
column.
If a person wants to return their results horizontally instead of vertically (as I have done), they would simply change the ROWS(A$2:A2)
portion of the formula to COLUMNS(A$2:A2)
and drag sideways the appropriate number of columns.
The original idea for this solution is from barry houdini as an answer to this question.
The way this formula works is:
- Evaluates if the search term box (
E2
for this example) is blank, if it is, it keeps the result cell blank as well. In this example, it is not blank, and it begins calculating.
- Next it searches for our search term ("ke" for this example) using the
SEARCH
function. It returns an array of Error messages and (for any matches) numbers of their position in the grid.
- Next, using the
ISNUMBER
function, it converts the values in the array to "FALSE" and "TRUE" based on what the SEARCH
returned in the previous step. In our example, this cell will be returning a "TRUE" in the Third position of the array.
- Since we have a "TRUE" response in our
IF
function, we will now evaluate what to return for that specific spot in the array. The ROW(A$7:A$28)-ROW(A$7)+1
section converts all the absolute row values of our array into relative values based on our array starting location.
- Inside our
SMALL
function we now have an array containing "FALSE" and any relative positions of the matches (in this example "3").
- The next evaluation step is our "nth counter" at the end of our formula. Because we are in the position that would output our first result (
A2
), it will return the number "1".
- Our
SMALL
function evaluates, and returns the first smallest result ("FALSE" is eliminated, and the number returned is the 3 from step 5).
- Our
INDEX
function now evaluates, and returns the cell in the third position of the array A$7:A$28
which is "Mickey Mouse".