Retrieve nth match with VLOOKUP
Suppose you have a list of values and their corresponding information, to retrieve the related information of the nth match of a value, you can use a formula based on the VLOOKUP function.
How to retrieve information associated with the nth match of a value with VLOOKUP?
To use the VLOOKUP function to get the information about the 1st, 2nd, and 3rd matches of Pencil in the table as shown above, you should first add a helper column to the left of your table with a COUNTIF formula as shown below. The helper column joins the products with the number of times that the products showed up. Then you can easily get the corresponding quantities of the nth matched pencils.
Formula in cell A5: =B5&":"&COUNTIF($B$5:B5,B5).
To get the information about the 1st match of Pencil, please copy or enter the formula below in the cell G7, and press Enter to get the result:
√ Note: The dollar signs ($) above indicate absolute references, which means the lookup_value and lookup_array in the formula won't change when you move or copy the formula to other cells. However, there are no dollar signs added to the cell reference of n since you want it to be dynamic. After entering the formula, drag the fill handle down to apply the formula to the below cells.
Explanation of the formula
Here we use the formula below as an example:
- The range_lookup FALSE asks the function to perform an exact match.
- The col_num 3 indicates that the matched value will be returned from the 3rd column of the range $A$5:$C$11.
- The snippet $F$4&":"&E8 indicates to lookup the 2nd (value in E8) match of Pencil (value in $F$4). In text form, the snippet is like this: Pencil:2.
- VLOOKUP now retrieves the corresponding quantity information from the 3rd column of the range $A$5:$C$11 that matches Pencil:2, which is 478.