Explanation

Note: in the current version of Excel, the FILTER function is a better way to solve this problem. The INDEX and MATCH formula explained here is meant for legacy versions of Excel that do not provide the FILTER function.

The core of this formula is the INDEX function, with AGGREGATE used to figure out the “nth match” for each row in the extraction area:

INDEX(data,nth_match_formula)

Almost all of the work is in figuring out and reporting which rows in “data” match the search string, and reporting the position of each matching value to INDEX. This is done with the AGGREGATE function configured like this:

AGGREGATE(15,6,(ROW(data)-ROW($B$5)+1)/ISNUMBER(SEARCH(search,data)),F5)

The first argument, 15, tells AGGREGATE to behave like SMALL , and return nth smallest values. The second argument, 6, is an option to ignore errors. The third argument is an expression that generates an array of matching results (described below). The fourth argument, F5, acts like “k” in SMALL to specify the “nth” value.

AGGREGATE operates on arrays, and the expression below builds an array for the third argument inside AGGREGATE :

(ROW(data)-ROW($B$5)+1)/ISNUMBER(SEARCH(search,data))

Here, the ROW function is used to generate an array of relative row numbers , and ISNUMBER and SEARCH are used together to match the search string against values in the data, which generates an array of TRUE and FALSE values.

The clever bit is to divide the row numbers by the search results. In a math operation like this, TRUE behaves like 1, and FALSE behaves like zero. The result is row numbers associated with a positive match are divided by 1 and survive the operation, while row numbers associated with non-matching values are destroyed and become #DIV/0 errors. Because AGGREGATE is set to ignore errors, it ignores the #DIV/0 errors, and returns the “nth” smallest number in the remaining values, using the number in column F for “nth”.

Managing performance

Like all array formulas, this formula is “expensive” in terms of resources with a large data set. To minimize performance impacts, the entire INDEX and MATCH formula is wrapped in IF like this:

=IF(F5>ct,"",formula)

where the named range “ct” (D8) holds this formula:

=COUNTIF(data,"*"&search&"*")

This check stops the INDEX and AGGREGATE portion of the formula from executing once all matching values are extracted.

Array formula with SMALL

If your version of Excel does not have the AGGREGATE function, you can use an alternative formula based on SMALL and IF:

=IF(F5>ct,"",INDEX(data,SMALL(IF(ISNUMBER(SEARCH(search,data)),ROW(data)-ROW($B$5)+1),F5)))

Note: this is an array formula and must be entered with control + shift + enter.

FILTER function

The INDEX and MATCH formula explained above is meant for legacy versions of Excel that do not offer the FILTER function . In the current version of Excel, a better way to solve this problem is to use FILTER in a formula like this:

=FILTER(data,ISNUMBER(SEARCH(search,data)))

As you can see, this is a much simpler formula . For a detailed explanation, see this page .

Explanation

In this example, the goal is to calculate the correct grade for each name in column B using the score in column C and the table to the right. For convenience only, grade (G5:G9) and score (F5:F9) are named ranges . This is a classic “approximate-match” lookup problem because it is unlikely that a score will be found in the lookup table. Instead, the table acts to group scores into buckets. For a given score, we want to match the largest value in the table that is less than or equal to the score. This problem can be solved with an INDEX and MATCH formula like this:

=INDEX(grade,MATCH(C5,score,1))

Background reading

This article assumes you are familiar with INDEX and MATCH. If not, see:

  • How to use INDEX and MATCH - overview with simple examples

The MATCH function

The MATCH function locates a value in a set of data and returns its position . In this example, the formula uses MATCH to find the correct row for a given score. MATCH is configured to look for the value in C5 in the named range score (F5:F9):

MATCH(C5,score,1) // returns 3

Here, the lookup_value is the score in C5, the lookup_array is the named range score (F5:F9), and match_type is set to 1, for an approximate match. A match type of 1 will cause MATCH to find the first value in the table that is less than or equal to the lookup value in C5. In this case, the score is 79, so MATCH returns 3. Now that we have the correct row number, we need to retrieve the grade in that row. This is a job for the INDEX function.

The INDEX function

The INDEX function is designed to retrieve values at a known location. In this example, the MATCH function returns a result of 3 directly to the INDEX function as the row_num argument. Once MATCH returns 3 we can simplify the formula to:

=INDEX(grade,3) // returns "C"

With array provided as the named range grade (G5:G9) and a row number of 3, INDEX returns the value at the 3rd row in G5:G9, which is “C”.

Note: For MATCH to work correctly with match_type of 1, the scores in F5:F9 must be sorted in ascending order. For more information, see our MATCH function page .