Google-sheets – Formula in Google Sheets to lookup contents of another cell

google sheetsvlookupworksheet-function

I'm trying to create a formula that brings back a certain cell depending on what another cell has in it. But the formula I'm trying isn't working. It's working for other things (what I thought was the same thing), but for some reason it's not working for this one.

=IF(ISNA(VLOOKUP($E21,Data!$A:$A,1,FALSE)),"",VLOOKUP($E21,Data!$B:$B,1,FALSE))

I have that formula working on several other cells and it's working fine. But when I try and get it to bring back a certain cell based on another, it's not working.

Here is a link to a copy sheet I'm working on.

Want I want:

When cell E21 gets typed in bringing up a certain size, cells E23-E26 are automatically populated with the item number coming from the Data sheet (column A). And then G23-G26 need to be populated with bundle count, from the same sheet, column c.

I've been trying all sorts of different formulas, but honestly, I don't understand most of them.

How can I get this to work?

Best Answer

Let's look first at why the function you've posted isn't working. VLOOKUP needs a two-dimensional data range to work with. It looks for your search_key (param 1) in the first column of range specified (has to be the first column - an unfortunate limitation of VLOOKUP), and returns a value from the matching row. The column returned is specified by param 3, index, essentially 'nth column in my range' (so 1 = your search key).

To get around the first column limitation, you could create an extra column with the data you want to return that is to the right of your search_key column. But it's messy, and looking at your data it won't help anyway.

That's because VLOOKUP only ever returns one value. If you have multiple matches for the search_key, it only gives you the first one it finds. I see that you have a few sizes of product with multiple part numbers, so we're going to need a different function.

I'd suggest FILTER for this job. What FILTER does is take a 2D data range, allow you to filter it down based on any column or row within that range, and return all the matching data - it actually fills neighbouring cells if there's enough data. I always think it's a bit weird to think of a function in a cell that actually acts on other cells, but that's how it works.

So what we could do is (in cell E23 ONLY):

filter(Data!A3:B2000,Data!B2:B2000 = E21)

To break that down, look in all of Data for rows where the value of column B matches E21.

Problem is that will return too many columns, and potentially too many rows. If there's data in the neighbouring cells that Sheets is going to put data into, the function will just fail. So we can use the ARRAY_CONSTRAIN function to limit the result of the filter:

=iferror(array_constrain(filter(Data!A3:B2000,Data!B3:B2000 = E21),4,1))

Fortuitously the data we want is in the left column of the range, so we just have to limit it down to one column (last param), and four rows (as you only have four rows available - you could add more if you like).

I've also added an IFERROR to blank it out if there are no results. It's not strictly what it does, but it's much cleaner than using an IF function to check ISNA, and it does the job.

You don't need to put any formula in the other three cells - the FILTER (limited by ARRAY_CONSTRAIN) will put the data there for you.

From there I'd say the easiest way to retrieve the bundle value is with a VLOOKUP:

=iferror(vlookup(E23,Data!A$3:C$2000,3,false),"")

So, look in our data range for the item value in E23, then return the third column. There might be faster ways, but this is clean, easy, and for a relatively small amount of data I wouldn't worry about the speed of it. Again, wrapped with an IFERROR. I just filled down for the other three rows - something I always get caught out by with VLOOKUP is forgetting to put the $s in where appropriate.

Here's the copy I made to work on, with the formulas filled in to see: https://docs.google.com/spreadsheets/d/1yKv6f9ciySvSPJ0Iy2AmfehpO7dVj38HWnlS2slrc5M/edit?usp=sharing

Unfortuately I don't have enough reputation to post links to each of the functions full documentation, but you can find them all here: https://support.google.com/docs/table/25273?hl=en

Hope that helps!