Move Index Column If Blank in VLOOKUP in Google Sheets

Published on

If the index column is blank, the VLOOKUP function will return an empty cell in Google Sheets. How can you retrieve the next non-blank value?

I’m referring to moving or shifting the index column in the VLOOKUP formula when the result is blank. This is a common dilemma faced by VLOOKUP users, whether they’re using Google Sheets or Excel.

Even if the search key is present in the lookup range, VLOOKUP may return a blank result. This can happen if there is no value to return in the corresponding cell of the found row.

Understanding the Problem

For example, I want to return a value from column 2 in the range, so I specify index number 2 in the VLOOKUP formula. However, if the corresponding cell in column 2 is blank, I want to get the value from column 3. If column 3 is also blank, then I want to pull the value from column 4, and so on.

I don’t want to manually change the index number in the VLOOKUP formula (e.g., changing from 3 to 4 to get the value from the fourth column). How can I automate this process?

You might consider searching for the key and returning all the values in the found row using a sequence of index numbers and entering the formula as an array formula. But that only partially solves the problem. You want the value from a specific column, and if it’s blank, you want the value from the next non-blank column — not all the values.

This approach is particularly useful in scenarios where you look up a value and return the corresponding value from a specific month or quarter column. If that value is empty, you want to retrieve the value from the next available month or quarter.

Consider the following table:

Sample Data

I want to use VLOOKUP to find Mike’s sales in Q1. The index column will be 2 in the VLOOKUP, and the formula would be as follows:

=VLOOKUP(G2, A2:E, 2, FALSE)

This formula will return a blank because the corresponding cell is empty. Therefore, you need the result from Q2. To get that, you would need to specify index number 3.

You can automate this process to dynamically adjust or move the index column in VLOOKUP in Google Sheets.

Formula Example of Moving the Index Column If Blank in VLOOKUP

In the above example, the data is in the range A1:E, and the lookup range is A2:E, excluding the header row.

Specify the salesperson’s name (the search key) in cell G2.

Then, enter the following formula in cell H2:

=IFERROR(
   INDEX(
      TOROW(
         VLOOKUP(G2, A2:E, SEQUENCE(COLUMNS(A2:E)+1-2, 1, 2), FALSE), 3
       ), 0, 1
   )
)

Where:

  • Search key: G2
  • Range: A2:E
  • Index: 2

Note: You can simplify this formula using the LET function, and I will share that after the formula explanation.

When you use this formula for a different range, you only need to modify these arguments. I’ve highlighted them in the formula for your convenience.

Example of moving the index column if blank in VLOOKUP in Google Sheets

Even though the index is set to column 2, if VLOOKUP returns an empty value, it will fetch the value from the next non-blank cell in that row.

Let’s break down this formula to understand how we dynamically move the index column if it’s blank in the VLOOKUP formula in Google Sheets.

Formula Breakdown

The formula consists of three parts: the VLOOKUP part, the TOROW part, and the INDEX part. These work together to dynamically adjust, or we can say, move the index column if it is blank in VLOOKUP. Let’s break down each part in detail.

VLOOKUP Part:

VLOOKUP(G2, A2:E, SEQUENCE(COLUMNS(A2:E)+1-2, 1, 2), FALSE)

This follows the syntax:

VLOOKUP(search_key, range, index, [is_sorted])

Where:

  • search_key: G2
  • range: A2:E
  • index: SEQUENCE(COLUMNS(A2:E)+1-2, 1, 2) – This generates a sequence of numbers corresponding to the columns in the range, starting from the specified index column. For example, if the index column is 2 and the range contains 5 columns, the formula will return the sequence numbers 2, 3, 4, and 5.
  • is_sorted: FALSE

This VLOOKUP formula searches for the value in cell G2 in the first column of the range and returns the corresponding values from the columns specified by the index (in this case, columns 2 to 5).

When testing this formula standalone, you would need to wrap it with ARRAYFORMULA since the VLOOKUP index contains multiple columns. However, in our formula, we omit ARRAYFORMULA because the INDEX function works as an array formula.

TOROW Part:

TOROW(..., 3)

The purpose of the TOROW function is to remove any empty cells or errors in the result, if they exist, from the VLOOKUP.

INDEX Part:

INDEX(..., 0, 1)

The INDEX function extracts the first value from the row returned by the TOROW function.

This is how we dynamically move the index column if it’s blank in the VLOOKUP function in Google Sheets. Now, let’s simplify the formula using the LET function to make it easier to specify the arguments.

Simplified Formula for Moving Index Column If Blank in VLOOKUP

You can simplify the above formula using the LET function. With this function, we assign names to the search key, range, and index column, making it easier to specify them.

Here you go!

=LET(
   search_key, G2, range, A2:E, col_index, 2, 
   IFERROR(
      INDEX(
         TOROW(
            VLOOKUP(search_key, range, SEQUENCE(COLUMNS(range)+1-col_index, 1, col_index), FALSE), 3
         ), 0, 1
      )
   )
)

In this formula, you should specify:

  • search_key: G2
  • range: A2:E
  • col_index: 2 (This represents the index; I used the name “col_index” to avoid conflicting with the INDEX function.)

That’s all about moving the index column if blank in VLOOKUP.

Sample Sheet

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.

How to Extract Numbers from Text in Excel with Regex

You can use the REGEXEXTRACT or REGEXREPLACE functions to easily extract numbers from text...

Using OFFSET and MATCH Together in Google Sheets: Advanced Tips

One powerful and flexible way to look up values is by combining the OFFSET...

How to Use OFFSET and XMATCH Functions Together in Excel

We often use the OFFSET and XMATCH functions together to match a value in...

How to Calculate Maximum Drawdown in Excel and Google Sheets

You can use the following dynamic array formula to calculate maximum drawdown (MDD) in...

More like this

Using OFFSET and MATCH Together in Google Sheets: Advanced Tips

One powerful and flexible way to look up values is by combining the OFFSET...

Running Count with Structured References in Google Sheets

Running a count with structured references is achievable in Google Sheets tables using the...

Running Total with Structured Table References in Google Sheets

You can use two types of formulas to create a running total with structured...

6 COMMENTS

  1. Hi there!

    Thanks for all you do!

    This was super helpful to get me going. I have an issue with my formula. I keep getting the following error:

    “MATCH range must be a single row or a single column.”

    In my case, the search key is a range of names from one sheet, and I’m searching for those names in the current sheet.

    Once the name is found, a value is returned. However, if the value in one row is blank, I want it to search the next row. Here is where I get the error because Match doesn’t appear to like it.

    This is the formula I’m using:

    =arrayformula(vlookup($F3:F,coinPrice!A2:C,match(false,
    isblank(vlookup(F3:F,coinPrice!A2:C,
    column(coinPrice!$B2:$C2),0)),0)+1,0))

    Any help will be greatly appreciated!

  2. Thanks for all your great formula help!

    I’m trying to use Vlookup to get the last non-blank item in a row rather than the first one.

    I tried to adapt your formula, but I can’t figure out how to get it to check in the other direction. Any help would be appreciated. 🙂

  3. Hi,
    This is almost what I was looking for, just with a little more detail.
    What if in your depicted table (with your VLOOKUP formula) I wanted to get the sum of the first 2 months, i.e., Jan and Feb. Also, if the Jan column is empty, get the sum of the subsequent 2 months.

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.