where codes (C5:H15) and group (B5:B15) are named ranges. The result for the lookup code “BDBC” is “Epsilon. This lookup can also be done in older versions of Excel with INDEX and MATCH, as explained below. where codes (C5:H15) and group (B5:B15) are named ranges. At a high level, this formula uses the XLOOKUP function to perform the lookup, with the number 1 as the lookup value, and the named range group as the return array. The tricky part of the formula is the lookup array, which is created with the MMULT function like this: The MMULT function performs matrix multiplication, which is a handy way to reduce results from many columns to a single column of results. MMULT takes two arrays, array1 and array2, and requires that the number of columns in array1 be the same as the number of rows in array2. The resulting matrix (which is an array) will have same number of rows as the first matrix, and the same number of columns as the second matrix. The first array is simply the logical test of all codes in the named range C5:I13 against the code we are looking for in K5: The double negative forces TRUE and FALSE values to 1s and 0s. The result is an array of 11 rows by 6 columns. Notice the lone 1 in row 5 is the only matching code: The first array has 6 columns, so the second array must contain 6 rows. The SEQUENCE function provides an easy way to construct this array, with some help from the COLUMNS function: COLUMNS returns 6 to SEQUENCE for as the rows argument. The columns argument is 1, start is 1, and the step value is zero. The result is an array with 6 rows and 1 column, filled only with 1: The MMULT function then calculates the matrix product of the two arrays and returns an array with 11 rows and 1 column: Notice row 5, which contains the code “BDBC” is 1, while all other rows are zero. This array is returned to XLOOKUP as the lookup_array: and XLOOKUP matches the 1 and returns the 5th item in group, “Epsilon”
Get matching column
The formula above can be adjusted to return the position or heading of the matching column. To get the matching column number from the range C4:H4, you can use a formula like this: The structure of this formula is similar to the formula above. The lookup_value is 1, and the lookup_array is created with MMULT here: Notice in this case we are using the ROWS function in the first argument to MMULT, and SEQUENCE is configured to create columns. The result from SEQUENCE is an array with 11 columns like this: The second array in MMULT is created with the same code we used above: After MMULT runs, it returns the following array directly to the XLOOKUP function as the lookup_array: Notice the 1 corresponds to column 3 in codes. We can now simplify the formula as follows: XLOOKUP locates the 1 in the array, and returns the third value in the range C4:H4, which is 3. Note that the range C4:H4 could be a named range, and can contain values of any kind.
Without XLOOKUP and SEQUENCE
In versions of Excel without XLOOKUP and SEQUENCE, this problem can be solved with a formula based on INDEX and MATCH: Note: this is an array formula and must be entered with control + shift + enter, except in Excel 365. The MMULT function is configured to return the same result as above, but the syntax used to create array2 is different: The COLUMN function returns 6 numbers in a horizontal array: And these numbers are then raised to a power of zero with the exponent operator (^): Raising any number to the power of zero (0) results in 1, so the result is the same 1 x 6 array filled only with 1s. Finally, TRANSPOSE flips the array to a 1 x 6 array to a 6 x 1 array: and the result is handed off to the MMULT function as array2, as before. The first array in MMULT is created in the same way as in the original formula, with the same result. So the final result from MMULT is the same: Substituting this array into the formula, we have a “standard” INDEX and MATCH formula: The MATCH function returns 5, the position of the single 1 in the array: And INDEX returns the 5th item in the named range group (B5:B15), “Epsilon”, as the final result.
Dave Bruns
Hi - I’m Dave Bruns, and I run Exceljet with my wife, Lisa. Our goal is to help you work faster in Excel. We create short videos, and clear examples of formulas, functions, pivot tables, conditional formatting, and charts.