Vlookup with Hlookup wrong cell value (Excel) - excel

I'm trying to use VLOOKUP with HLOOKUP to extract a value from table but it always give me wrong cell - offset the cell value by one -
The equation is
=VLOOKUP(G22,A5:Z18,HLOOKUP(H22,B3:Z4,1,FALSE),FALSE)
the cell in red rec. is the right answer, but it always return value in green circle
What is wrong with my code?

INDEX/MATCH Over VLOOKUP or HLOOKUP
As a rule of thumb, using HLOOKUP only makes sense if you have more rows and you want to return a value from any but the first row. Similarly using VLOOKUP only makes sense if you have more columns and you want to return a value from any but the first column.
A more flexible handling of lookups is achieved by using INDEX with MATCH.
=INDEX(A3:Z18,MATCH(G22,A3:A18,0),MATCH(H22,A3:Z3,0))
If someone enters 3 in G22, an error will be displayed. A simple way of error handling is using the IFERROR function.
=IFERROR(INDEX(A3:Z18,MATCH(G22,A3:A18,0),MATCH(H22,A3:Z3,0)),"")
Study the image closely. When it comes to finding exact matches, I never use VLOOKUP or HLOOKUP because INDEX with MATCH covers it all and more. That doesn't mean that you should abandon using them because they are good tools to get familiar with indexes, offsets, and whatnot.

If you have Excel 365, you can use Xlookup as described in Example 5 of This reference. Note that Xlookup can return a whole column from a 2d array:
=XLOOKUP(G22,A5:A18,XLOOKUP(H22,B3:K3,B5:K18))
or if the data is in fact a table where the Phi is the first column and the mm are the headers, this would be:
=XLOOKUP(G22,Table1[φ],XLOOKUP(H22,Table1[#Headers],Table1))
Note If the mm are in the headers, they are formatted as text so H22 has to be formatted to reflect this or use
=XLOOKUP(G22,A5:A18,XLOOKUP(TEXT(H22,"general"),B3:K3,B5:K18))
and
=XLOOKUP(G22,Table1[φ],XLOOKUP(TEXT(H22,"general"),Table1[#Headers],Table1))

Related

Excel formula to return an adjacent value when two cells match

Here is what I'm looking for. I need to have Excel return a specific value from an adjacent key when 2 cells match. I attached 2 images here to explain what I'm looking to do. The first image is where I'm currently at and the second image is where I want to get to.
So for example I need Excel to match cell A1 to J1 and drop the value in K1 into cell E1 (red cell). I then of course need to run this down all of column E, asking excel to match the number in column A with a number in column J then dropping the appropriate dollar value into column E.
I've tried the VLOOKUP, INDEX, MATCH formulas and none of them seem to give me the answer.
You're on the right track - this is exactly what VLOOKUP and INDEX/MATCH are for!
Here's the solution in this case:
=INDEX($K$1:$K$10, MATCH($A1, $J$1:$J$10, 0))
I'd generally prefer INDEX/MATCH to VLOOKUP especially if you plan to add columns to your spreadsheet. If you however prefer the VLOOKUP solution, it's as #Warcupine commented:
=VLOOKUP($A1,$J$1:$K$10,2,FALSE)
If you're rocking a newer version of Excel (2019+), XLOOKUP provides the best of both worlds in my opinion (stable and concise):
=XLOOKUP($A1,$J$1:$J$10,$K$1:$K$10)

Vlookup from a fomula cell

I am trying to use vlookup to get a value from a cell but the cell I trying to get from has a formula and as a result the vlookup is resulting in "#N/A". Does anyone know why this happens please?
Thanks,
The fact that the range you are looking in has formulas should not have any bearing on the returned result.
When VLOOKUP returns an #N/A, it means that it was unable to find the specified value within the specified range. So, in your case, what ever is in C2, was not found in column B.
If you would like to catch these cases and prevent #N/A showing, you can wrap the VLOOKUP in an IFNA:
=IFNA(VLOOKUP(C2,A:B,2,false),"Not Found")
But I would strongly recommend that you NEVER use VLOOKUP.
INDEX/MATCH does exactly the same thing, is faster, more versatile, and less fragile (for example, VLOOKUP breaks if you insert a column within the range).
The INDEX/MATCH equivalent of your VLOOKUP would be:
=IFNA(INDEX(B:B,MATCH(C2,A:A,0)),"Not Found")

Excel - VLOOKUP vs. INDEX/MATCH - Which is better?

I understand how to use each method: VLOOKUP (or HLOOKUP) vs. INDEX/MATCH.
I'm looking for differences between them not in terms of personal preference, but primarily in the following areas:
Is there something that one method can do that the other cannot?
Which one is more efficient in general (or does it depend on the situation)?
Any other advantages/disadvantages to using one method vs. the other
NOTE: I am answering my own question here but looking to see if anyone else has other insights I hadn't thought of.
I prefer to use INDEX/MATCH in practically every situation because it is far more flexible and has the potential to be much more efficient depending on how large the lookup table is.
The only time when I can really justify using VLOOKUP is for very straight-forward tables where the column index number is dynamic, although even in this case, INDEX/MATCH is equally viable.
I'll give a few specific examples below to demonstrate the detailed differences between the two methods.
INDEX/MATCH can lookup to the left (or anywhere else you want)
This is probably the most obvious advantages to INDEX/MATCH as well as one of the biggest downfalls of VLOOKUP. VLOOKUP can only lookup to the right, INDEX/MATCH can lookup from any range, including different sheets if necessary.
The example below cannot be accomplished with VLOOKUP.
INDEX/MATCH has the potential to use smaller cell ranges (thus increasing efficiency)
Consider the example below. It can be accomplished with either method.
Both of these formulas work fine. However, since the VLOOKUP formula contains a larger range than the INDEX/MATCH formula, it is unnecessarily volatile.
If any cell in the range B1:G4 changes, the VLOOKUP formula must recalculate (because B1:G4 is within the range A1:H4) even though changing any cell in B1:G4 will not affect the outcome of the formula. This is not an issue for INDEX/MATCH because its formula does not contain the range B1:G4.
Using VLOOKUP with fixed col_index_number is dangerous
The main issue I see with having a fixed column index number is that it will not update as it should if full columns are inserted. Consider the following example:
This formula works fine unless a column is inserted within the lookup table. In that case, the formula will lookup the value to the left of where it should. See below, result after a column has been inserted.
This can actually be alleviated by using the following VLOOKUP formula instead:
= VLOOKUP("s",A1:H4,COLUMN(H1)-COLUMN(A1)+1,FALSE)
Now H1 will automatically update to I1 if a column is inserted, thus preserving the reference to the same column. However, this is entirely unnecessary because INDEX/MATCH can accomplish this without this problem with the formula below.
= INDEX(H1:H4,MATCH("s",A1:A4,0))
I realize this is an unlikely scenario, but it always bothered me that VLOOKUP by default looks up based on a fixed column index that does not automatically update if columns are inserted. To me, it just seems to make the VLOOKUP function more fragile.
INDEX/MATCH can handle variable column indexes just as well, but longer formula
If the column index number itself is dynamic, this is really the only case when I think VLOOKUP simplifies things a bit, but again the INDEX/MATCH alternative is just as good, just slightly more confusing. See below examples.
INDEX/MATCH is more efficient for multiple lookups
(thanks to #jeffreyweir)
If multiple lookup values are needed for a single match value, it is much more efficient to have a helper cell with the match value. This way, the match only has to be computed once, instead of one for each lookup formula. See example below.
This match value can then be used to return the appropriate lookup values. See example below, (formula has been dragged to the right).
This manual "splitting" of the match value and index values is not an option with VLOOKUP since the match value is an "internal" variable in VLOOKUP and cannot be accessed.
INDEX/MATCH can look up a range, allowing another operation
Let's say for example you want to find a max value in a column based on the column name.
You can first use MATCH to find the appropriate column, then INDEX to return the range of that entire column, then use MAX to find the max of that range.
See example below, the formula in H4 looks up the max value of the column name specified in cell G4. This cannot be accomplished using VLOOKUP alone.
MATCH doesn't have to match an exact value
Usually MATCH is used with the third argument as 0, meaning "find an exact match". But depending on the situation, using -1 or 1 as the third argument of MATCH can be very useful.
For example, the following formula returns the row number of the last row in column A that contains a number:
= MATCH(-1E+300,A:A,-1)
This is because this formula starts from the bottom of the A column and works its way toward the top, and returns the first row number in the A column where the value is greater than or equal to -1E+300 (which is basically any number).
Then INDEX can be used in combination with this to return the value in that cell. See example below.
In Summary
VLOOKUP is, at best, as good as INDEX/MATCH and admittedly slightly less confusing in some situations. And at worst, VLOOKUP is much more unsafe and volatile than INDEX/MATCH.
Also worth noting that if you want to look up a range instead of a single value, INDEX/MATCH must be used. VLOOKUP cannot be used to look up a range.
For these reasons, I generally prefer INDEX/MATCH in practically all situations.

EXCEL VBA or Function VLoopup - Based on Multiple Criteria to find the value

I want to Vlookup using two criteria:
the value in Column D;
The amount in Column A and Column G which can be approximately matched.
The normal Vlookup only returns the value of the first row, so one solution I think about is to match both the Invoice Number and Approximately Match the amount.
I am not sure if it is the best solution, please advise a better one, not necessarily in VBA, it can also use INDEX or MATCH functions, etc.
Thanks a lot in advance!
Below is the formula I've been trying, it is like something If true, then this value, if false then Lookup the next one.... But I am still thinking about how to navigate though the Vlookup to search next value, do I need to use VBA to solve this?! Vlookup based on Multiple Criteria2
The following formula should work:
=INDEX(G3:G5,SUMPRODUCT(MAX((D3:D5=B3)*ROW(D3:D5)))-3,1)
That's just a fancier version of Index(Match) that is used as a common replacement for vlookup. In this case, instead of Match we use Sumproduct to get the max row with the criteria we are looking for.
ADDED: Added a -3 to the second paramater of INDEX in order to adjust the Row() returned to the actual range that is being Indexed.

Simple Excel vlookup doesn't work

I am a programmer that rarely uses Excel. I'm now trying to do a simple vlookup and it just won't work. I have read several online tutorials and troubleshooting guides, no dice. Here's what I've got:
As you can see, the formula in B8 is =VLOOKUP(A8,$A$1:$B$5,1,FALSE)
I am baffled why this isn't working. I have absolutely verified that each cell in the lookup table (A1-B5) doesn't contain any leading/trailing spaces, no special chars, etc. In fact I typed these in manually, they're not pasted. Same goes for the little column of colors (A8-A11). This is the simplest case possible. For example, I want the formula in B8 to look at "Red" in A8, find Red in the lookup table, and return Red's number, which is "3". And I want an exact match.
In case you're wondering why I'm trying this on a simple and useless case, it's because I began on a more complex sheet, as part of prepping for a data import from Excel, got the #N/A everywhere, so I started a new worksheet and made this simple example, and got the same wrong result.
What am I doing wrong?
You would be better served by using index() and match() because in a vlookup(), the value that you're trying to look for has to be in the left-most column.
match() will return the number or index (in your case, the the row number) in which it finds the value you're looking for, and that can be given to index() to use to return some other value associated with that index (in this case, the color number in that row). It would end up looking like this:
=index($a$1:$a$5, match(a8, $b$1:$b$5, 0))
I found that the lookup value (color) must be in the left column and the ID must be in the right column.
VLOOKUP doesn't work to it's left, this function looks rightwards. This is why you need to swap numbers and colors.

Resources