Exclude Duplicate Keys in VLOOKUP Array Result in Google Sheets

How do we assign a VLOOKUP result only to the first occurrence of the search key in a range? This is usually required when using an array formula with VLOOKUP. To exclude duplicate search keys in a VLOOKUP array formula result, we can use an IF logical test combined with a running count.

The logic works as follows: We find the running count of the search keys. If the count is one, we use VLOOKUP to return the result; otherwise, we leave the cell blank.

Although this typically requires an array formula, we will begin with a non-array formula to help you understand the process better.

Example

In the following example, I have search keys in the range A2:A10, which are fruit names, and the lookup table is in the range F2:G6, where the first column contains fruit names and the second column contains their prices.

Looking at the search keys, you can see that some fruits are repeated. I want a VLOOKUP formula in cell B2 that excludes duplicate search keys and returns results only for the first occurrence.

Example of Excluding Duplicate Search Keys in VLOOKUP Array Result

Exclude Duplicate Search Keys in VLOOKUP Using a Drag-Down Formula

As per the example above, you can use the following formula in cell B2 and drag it down:

=IF(
   COUNTIF($A$2:A2, A2)=1,
   VLOOKUP(A2, $F$2:$G$6, 2, FALSE),
)

How does this formula work?

The COUNTIF formula, COUNTIF($A$2:A2, A2), returns the running count of the search key as you drag it down. As you drag the formula down, the range becomes $A$2:A3, $A$2:A4, and so on, with the criterion becoming A3, A4, etc. The formula returns the count of the current item up to that row, which is the running occurrence.

The IF function evaluates whether the COUNTIF result is 1, and if it’s TRUE, it executes the VLOOKUP; otherwise, it returns a blank.

This is how you can exclude duplicate search keys in a VLOOKUP drag-down formula. Now, let’s see how to achieve the same result with a VLOOKUP array formula that expands down from B2.

Exclude Duplicate Search Keys in VLOOKUP Array Formula Result

The formula follows the same logic as above. The difference is that you should specify the search key range A2:A10 in the VLOOKUP instead of just A2. You’ll also need to replace the COUNTIF formula with a COUNTIFS formula. Additionally, the formula must be entered as an array formula.

Empty the range B2:B and insert the following formula in cell B2:

=ArrayFormula(
   IF(
      COUNTIFS(A2:A10, A2:A10, ROW(A2:A10), "<="&ROW(A2:A10))=1, 
      VLOOKUP(A2:A10, F2:G6, 2, FALSE),
  )
)

Here’s how it works:

  • The COUNTIFS(A2:A10, A2:A10, ROW(A2:A10), "<="&ROW(A2:A10)) part returns the running occurrence of the VLOOKUP search keys.
  • The IF function returns TRUE wherever the COUNTIFS result is 1 and executes the VLOOKUP array formula in those rows.

This is how we can exclude duplicate search keys in the VLOOKUP array result in Google Sheets.

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Count Consecutive Duplicates in Excel (Dynamic Array Formula)

Counting consecutive duplicates in Excel is useful for analyzing patterns, detecting repetitive sequences, and...

How to Break RANK Ties Alphabetically in Google Sheets

The RANK function in Google Sheets is commonly used to assign rankings to numerical...

Google Sheets: Highlight an Entire Column If Any Cell Has an Error

Google Sheets allows you to highlight an entire column if any cell has an...

Filter Top N per Category in Excel (Using FILTER & COUNTIFS)

You can use a combination of FILTER and COUNTIFS to filter the top N...

More like this

How to Break RANK Ties Alphabetically in Google Sheets

The RANK function in Google Sheets is commonly used to assign rankings to numerical...

Google Sheets: Highlight an Entire Column If Any Cell Has an Error

Google Sheets allows you to highlight an entire column if any cell has an...

Google Sheets: Extract Top N per Group from Query Aggregation

When working with grouped and aggregated data in Google Sheets, you might need to...

2 COMMENTS

  1. I am trying to use this with a question bank I have for google forms, but I am not putting in the right info to get it to work. Below is the Vlookup formula I am using, which pulls from another sheet with questions on it. Can you please help me fix this?

    =vlookup(randbetween(1,max(questions!$A:$A)),questions!$A$2:$B,2,false)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.