Explanation
Working from the inside out, the ISERROR function returns TRUE when a value is a recognized error, and FALSE if not.
When given a range of cells (an array of cells) ISERROR function will return an array of TRUE/FALSE results. In the example, this resulting array looks like this:
{FALSE;FALSE;FALSE;FALSE;FALSE;TRUE;FALSE;FALSE}
Note that the 6th value (which corresponds to the 6th cell in the range) is TRUE, since cell B9 contains #N/A.
The MATCH function is configured to match TRUE in exact match mode. It finds the first TRUE in the array created by ISERROR and returns the position. If no match is found, the MATCH function itself returns #N/A.
Finding the first NA error
The formula above will match any error. If you want to match the first #N/A error, just substitute ISNA for ISERROR:
{=MATCH(TRUE,ISNA(B4:B11),0)}
Explanation
This formula depends on a TRUE or FALSE result from a logical test, where FALSE represents the value you are looking for. In the example, the logical test is data=“red”, entered as the lookup_array argument in the MATCH function:
=MATCH(FALSE,data="red",0)
Once the test is run, it returns an array or TRUE and FALSE values:
=MATCH(FALSE,{TRUE;TRUE;TRUE;FALSE;TRUE;FALSE;TRUE;FALSE},0)
With the lookup_value set to FALSE, and match_type set to zero to force and exact match, the MATCH function returns 4, the position of the first FALSE in the array.
Get associated value
To retrieve the associated value from the Quantity column, where “quantity” is the named range C5:C12, you can use INDEX and MATCH together:
{=INDEX(quantity,MATCH(FALSE,data="red",0))}
Literal contains
If you need to match the first value that literally “does not contain”, you can use a variant of the formula. For example to match the first value in data that does not contain an “r”, you can use:
{=MATCH(FALSE,ISNUMBER(SEARCH("r",data)),0)}
Note: this is an array formula and must be entered with control + shift + enter, except in Excel 365 .
For more details about ISNUMBER + SEARCH, see this page .