Is there an Excel formula with VLOOKUP in two columns - excel-formula

I wonder if there is a VLOOKUP formula that looks first in one column and if there is no match, proceeds to the next column and if there's still no match returns a value, for example, NO MATCH.
I've been trying to Google and YouTube the issue but couldn't find anything. Hope someone can help me here.

lets say you lookup table is is from A1:D6 with columns A and B your look up values. And the value you are looking for is in G6
start with your basic VLOOKUP function as follows:
=VLOOKUP(G6,A1:D6,3,0)
This assumes you want the value from the third column in your range (C in this case) to be returned because of the 3. You are also looking for an exact match as defined by 0.
If the value you are looking for is not found in column 1 it will produce an error. As such we can use the IFERROR function to tell us what to do when an error occurs.
=IFERROR(VLOOKUP(G6,A1:D6,3,0),VLOOKUP(G6,B1:D6,2,0))
So in the event of an error it performs the second lookup. notice the range for the look up starts in column B instead of A. Also notice that Column C is now the 2nd column in the range so the 3 changed to a 2. Now if its not found in either situation, we can simply nest the IFERROR inside another IFERROR as follows:
=IFERROR(IFERROR(VLOOKUP(G6,A1:D6,3,0),VLOOKUP(G6,B1:D6,2,0)),G6&" not found in first two columns")
This method will return the first value that matches your search criteria that it encounters starting from the top in column one and if that fails the first value from the top in column 2. It neither is found it will post an error message.
There are other ways to do this especially if there were a max of one instance of the look up value in the two columns or if you did not care about columns and just wanted the first/last row (or nth row) of the occurrence.

Related

Index/Sumproduct with 3 criteria

I am trying to lookup a value from another table based on a reference table.
See below my data sample:
SHEET 1 ("CalculationLiability"):
SHEET 2 ("KeyMetrics"):
In sheet 1, cell G7 I am trying to look up the value from Sheet 2 based on 3 criteria (supplier unique ID, type and season) I tried the following formula, but it is returning a #REF error.
=INDEX(KeyMetrics!$F$6:$AS$100,
SUMPRODUCT((KeyMetrics!$D$6:$D$39=CalculationLiability!$D7)*
(KeyMetrics!$E$6:$E$39=CalculationLiability!$G$6)*
(KeyMetrics!$F$5:$AS$5=CalculationLiability!$E7)))
Anyone knows what I am doing wrong here? I can get it to work for two criteria, but for three criteria it doesn't work. Any help or push into the right direction is much appreciated!
The Index uses a multi column, multi row reference. That means that you need two additional arguments, one argument for row, and another for column.
Your formula currently only provides one additional argument. When you step through the formula with the Evaluate Formula tool you can see that in the last step.
You can use an Index with two Match functions. The first one to find the row, the second one to find the column.
=INDEX(KeyMetrics!$F$6:$AS$100,
MATCH($D7&$G$6,INDEX(KeyMetrics!$D$6:$D$100&KeyMetrics!$E$6:$E$100,0),0),
MATCH(CalculationLiability!E7,KeyMetrics!$F$5:$AS$5))
You can also use Sumproduct, but in that case, don't use Index.
=SUMPRODUCT(KeyMetrics!$F$6:$AS$39,
(KeyMetrics!$D$6:$D$39=CalculationLiability!$D7)*
(KeyMetrics!$E$6:$E$39=CalculationLiability!$G$6)*
(KeyMetrics!$F$5:$AS$5=CalculationLiability!$E7))

In MS Excel, how do I find the nth value in vlookup when your range has duplicate lookup values

Ok, let me see if i can phrase this properly. I have a table with values I want to search that have duplicate items in the reference column but i want the lowest value in the adjacent column. I've sorted according to the reference column then the column with the values in ascending order. For normal look-ups the vlookup function returns the first row and that suits me for normal searches. The caveat here is that my reference column values contain wildcards and when it it finds similar items it returns the values from the last instance; here is my issue:
1. Item Value
2. abc* 3
3. abd* 5
4. abe* 4
5. abe* 6
I want my vlookup to return the first instance of abe* of 4 but it always gets the second or last instance of 6. Since I'm searching in items containing wildcards I use the parameter FALSE for the closest match. I don't understand why it wont return the first instance. Am I missing something? Is there a better function to do what I need?
I apologize if this is the wrong venue for my question. Please direct me to the correct one if I am in error.

Excel - Conditional row from an array to COUNTIF from VLOOKUP

I don't know if I'm going about this the wrong way but it seems like it should be simple. Column A has a list of Names. Along each row is several "W"'s. Another separate field has a drop down representing Column A names. I want to count the number of "W"'s in a row corresponding to what name I select. I've tried using VLOOKUP and COUNTIF but I can't figure out how to select the entire array and then single out the one row that matches my selected name. I can get it working with a bunch of IF statements but thats far too time consuming as I'm manually matching the name to the row (and it isn't future proof).
There are a few ways to first 'narrow in' on the row you're looking for, after which point you can use a simple COUNTIFS to check the number of W's in that row.
One method would be to simply use INDIRECT, and create the row reference on the fly, like so [assumes your search cell is C1]:
=COUNTIFS(INDIRECT(MATCH(C1,A:A,0)&":"&MATCH(C1,A:A,0)),"W")
This first uses MATCH to find the appropriate row, and then builds a reference to that row [like "24:24"], which becomes the row that INDIRECT passes to COUNTIFS, which counts that row for W's.
For only one use of INDIRECT, the high computing costs of INDIRECT should not be an issue.
Another method would be to point out the full possible box that data could be contained in [let's assume that at most only column H would be used], and then use INDEX to give us the appropriate row number, like so:
=COUNTIFS(INDEX(A:H,MATCH(C1,A:A,0)),0,"W")
This again uses MATCH to find the row which contains the value found in C1 within column A. Then it takes the full possible box from INDEX, and returns all columns from the particular row [note that telling index to return 0 for the column # actually returns all columns instead].
Other methods would be possible [for example OFFSET], but I believe these two show the principle fairly well.
You could use the "Helper" Column method:
In the helper column:
=COUNTIF(B2:H2,"W")
Then use SUMIF() in the totals column:
=SUMIF($A$2:$A$9,K2,$I$2:$I$9)

Clarification of parameters in an INDEX MATCH formula

I am working through a spreadsheet created by someone else and in one Worksheet Column A has a value obtained with the formula below;
=INDEX(Sites!A$2:AC$10000,MATCH(F2,Sites!P$2:P$10000,FALSE),3)
I am confused by this formula and wondered if someone could clarify it for me please. I have never used an INDEX MATCH formula before and when I google for it, there are no examples that use a FALSE or TRUE before the end value, in this case "3". Also if the last value is "3" or the third column in the lookup range - how can this be when the P$2:P$10000 only has one column, Column P?
Here is a screen image of the worksheet
The FALSE works but is wrong.
With the MATCH function the 3rd parameter is optional. If not specified it defaults to the value of 1, which tells the MATCH function that the data to search is sorted and to therefore use a Binary Search algorithm to do the matching... which is really fast.
A value of zero, tells the MATCH function to instead do a linear search from top to bottom until the value is found, or not found at the end. This is called an Exact-Match search. The value 0 should be used here. It just so happens that FALSE evaluates to zero, but there is no reason to use it here, and it is just misleading. It should be simply 0.
...
Regarding the 3 at the very end. This instructs the INDEX function to return the corresponding value in the THIRD column of: Sites!A$2:AC$10000.
Index Match is a pretty powerful combination. It will return a value from an Index (which you set via a range), then uses a Match() to get the row.
In your example, the formula will return some value in the range A2:AC10000. [I think though this needs to be edited to just A2:A10000?]
Great, but which cell in that range? We need to know a row. Think of it like getting a cell in a cross section. The Index part is the column, and now you need a way to say what ROW to use. This is where Match() comes in.
Match first uses some value you want to find. In your example, it's looking at the building name ("Kilibarachan Primary"). This name exists in a column somewhere, Column P. It will find that factory name in column P, and return the row number. This row number is then fed to the Index. Now you have the column (A) and the intersection row.
Edit: Excel Hero beat me to this, but I figured I'd leave it anyways.

How this lookup formula works?

I'd like you to explain why this formula works: =LOOKUP(2,1/(A2:A10=D2),B2:B10)
I know about lookup, I know what this formula does (The following formula searches A2:A10 for the last instance of the value in D2, and returns the corresponding value from B2:B10...) but I don't understand how it works. For example what does this part do: A2:A10=D2 ?
Here is the source where you can find the workbook also: http://www.xl-central.com/lookup-last-instance.html
LOOKUP(lookup_value, lookup_vector, [result_vector])
The LOOKUP function is a very basic cousin of the HLOOKUP function and VLOOKUP function. The latter two provide additional functionality for selecting the row/column of data to return from their table_array parameter (row_index_num/col_index_num) as well as a range_lookup option which can force an exact match or an approximate (aka nearest) match when used with sorted data. The LOOKUP function has to have the result_vector (return row or column) specified if it is not the same as the lookup_vector and always returns the nearest match.
Excel treats TRUE as one and FALSE as zero when used mathematically. A 1 divided by 1 will equal 1 and a 1 divided by 0 is a #DIV/0! error. A #DIV/0! error will not match anything; not even another #DIV/0! error. Since you are looking for a 2, it finds something that might be the best match in row 2 (e.g. 1/1) but keeps looking since it is not an exact match. It finds another possible in row 5 but keeps looking. It finds another possible in row 8 and cannot find anything remotely close below that so it returns the value from column B in row 8. We are looking to match a 2 with a series on 1's and errors because we want the last nearest match.
For all intents and purposes, you are breaking the rules when using this type of formula as the lookup_vector is not in ascending order (which conventionally it should be). With the unsorted duplicate values we are achieving the correct results by relying on the function's 'broken' behavior when looking for a value it will never find; e.g. looking for a 2 in an array of 1's and errors.

Resources