Set condition before running Vlookup VBA - excel

I created a Vlookup but first want a condition to be met to determine what Vlookup to use.
Such as
If cell = 1 then
run Vlookup #1
If cell = 2 then
run Vlookup #2
There are only 4 possible variables that the cell could equal. This would have to be on a loop, because the entries in the worksheet are multiple. It would first see what the specific cell equaled, then determine what Vlookup to use.
Any insight?

I doubt you need a loop because this might be handled within the VLOOKUP, though I am assuming the conditional cell (one of four values) is not the trigger cell for the VLOOKUP:
Here A1 is the condition, C1 the trigger value and D1 contains the formula for the lookup:
=VLOOKUP(C1,$H$1:$L$3,A1+1,0)
and hence also the result k in the example.
A2 merely determines how many columns across to step when looking for the appropriate value in the lookup table (here in the box).
We know from ColumnH that 20 is in the second row of the table and A1+1 says take the value A1 (ie four) columns further to the right.
Change the blue to 2 and the yellow to 30, for example, and the result is f.

If values are for sure integers 1 to N, just like #Tim pointed out you can use CHOOSE:
=CHOOSE(A1;Vlookup1;Vlookup2;...;VlookupN)
If A1=1 Vlookup1 will be executed, A1=2 Vlookup2....A1=N... VlookupN

Related

Conditional Formatting Based On Multiple Columns

Sheet1
[
Sheet2
[
On Sheet 1 I have four columns Col A= 1st adjustment, Col B= 1st Check, Col C= 2nd Adjustment, Col D= 2nd Check.
The values in these columns are either 0 or non 0. Always a number. For the two adjustment columns I format them to be Red (if they are non-zero) and for the two check columns I format them to be Yellow (if they are non-zero).
On sheet 2 there is a column that pulls and combines the the sets of adjustments and checks and assigns them to the correct employee ID #.
Somehow I need the formatting on the column in sheet 2 to be red if the number came from an adjustment column on the other page and yellow if the number came from a check column on the other page. And no color if it is equal to 0.
What is the best way to do this? I have not been able to find a solution.
The conditional formatting rules I have are all applied to column C Sheet 2. They are:
#1- Cell value equal to = 0 (no fill)
#2 Cell value equal to =Sheet1!$E1 (yellow fill)
#3 Cell value equal to =Sheet1!$C1 (yellow fill)
#4 Cell value equal to =Sheet1!$D1 (red fill)
#5 Cell value equal to =Sheet1!$B1 (red fill)
Assuming that both lists start in A1, put this formula in cell D2 of Sheet2:
=IF(C2=0,0,IF(C2=INDEX(Sheet1!$B$2:$E$7,B2,(A2-1)*2+1),1,2))
Then select the range C2:C13 and open the conditional formatting menu. Add 3 new rules based on formula. The first formula is:
=D2=1
and set the format as filled in red. The second formula is:
=D2=2
and set the format as filled in yellow. The third formula is:
=D2=0
and set the format as unfilled (which is not the same as no format selected). This last rule is not strictly necessary if none is going to change the format of your range manually. Still...
Note that each formula for the conditional formatting is written without any $ that would lock the reference. Therefore each cell in the range $C$2:$C$13 will translate that formula relatively to its own position. Cell C2 will "look into" cell D2, cell C3 will look into cell D3, C4 into D4 and so on. You can of course hide column D and the conditional formatting will still work.
You could also make it without using an extra column. Just edit the formula meant for cell D2 accordingly and use it in your rules. Something like this should work:
=IF(C2=0,0,IF(C2=INDEX(Sheet1!$B$2:$E$7,B2,(A2-1)*2+1),1,2))=1
=IF(C2=0,0,IF(C2=INDEX(Sheet1!$B$2:$E$7,B2,(A2-1)*2+1),1,2))=2
=IF(C2=0,0,IF(C2=INDEX(Sheet1!$B$2:$E$7,B2,(A2-1)*2+1),1,2))=0
Formula details (as asked in comments)
B2 is for the row. In the context of the INDEX function it says at what row of the range ($B$2:$E$7 in the formula we see here) the INDEX function has to "look". If B2 is 1, it will consider the first row of the range (in our case row 2). If it's 2, it will consider the second row (in our case row 3).
(A2-1)*2+1 stands for the column. In the context of the INDEX function it says at what column of the range ($B$2:$E$7 in the formula we see here) the INDEX function has to "look". If the result is 1, it will consider the first column of the range (in our case the B column). If it's 2, it will consider the second column (in our case the C column).
About (A2-1)*2+1: let's change it a bit so it will be easier to visualize while i explain it:
1+(A2-1)*2
We can divide the formula in 2 parts: the 1 which stand as our starting point (the first column) and the (A2-1)*2+1 which deal with the shift in columns according to the shift in weeks. In cell A2 we find the number of the week. In the first sheet we have 2 columns for each weeks. Therefore when the week shifts by 1, our target column will have to shift by 2. We can't just multiply by 2 the week sheet because it's the shift from the first week that counts for 2, not the actual value. At the first week we will have no shift. Therefore our shift will have to be 0; but we can't just use a 0 because there is no "zero column" in a range. Therefore we need to have our starting value of 1 that is summed to the shift. This way for the first week we will have:
1+(1-1)*2
1+(0)*2
1+0
1
First column of the range $B$2:$E$7 (which is column B). For the second week we will have:
1+(2-1)*2
1+(1)*2
1+2
3
Third column of the range $B$2:$E$7 (which is column D).
Over all the formula IF(C2=INDEX(Sheet1!$B$2:$E$7,B2,(A2-1)*2+1),1,2) basically checks if the value in C2 is the same as the first column of the given week. If it's the same value it returns 1, otherwise it returns 2 (basically assuming that the value will be equal to the one in second colum of the given week).
Report any question you have or bug you have encountered. If, according to your judgment, this answer (or any other) is the best solution to your problem you have the privilege to accept it (link).
Let me give you a general approach for conditional formatting, based on a formula:
First you try to set up your formula, which you enter in some cell. The result of the formula should be TRUE in case you want the formatting to be applied, and FALSE in all other cases.
Please edit your question again, showing the formula you have tried for configuring this.
Oh, you just want to copy the formatting of that first sheet. But tell me, how is the formatting of that first sheet determined? Is it also the result of a conditional formatting? And about the numbers of the ADJ/Check column, is that just the sum of the values in the first sheet?

Excel Formula to find the first higher value

In column D (Result), I would like to have the following formula.
For each Cell in column C, find in column B the first value higher than the value of the cell of column C (starting from the same row) and gives as output the difference between the values found in column A (Count).
Example:
the value in C2 is 40. the first cell of B that has a value higher than 40 is B6. So D2 takes A6.value - A2.value = 5 - 1 = 4.
Can it be done without the use of VBA?
It can easily be accomplished with an array formula (so you have to enter the formula with Ctrl+Shift+Enter ) :
{=MATCH(TRUE;IF(B2:$B$7>C2;TRUE;FALSE);0)-1}
Put this formula in cell D2, and just drag down. You only have to change the end of your data set (change $B$7 into the real last cell of the column with data)
The formula works as follows :
The IF statement results in an array with TRUE/FALSE values that meet your criteria : {FALSE;FALSE;FALSE;FALSE;TRUE;FALSE}
The MATCH (with the 0 switch) searches the array for the index of the first match, which is 5 in our case
And you have to subtract 1 to get the offset to the cell where the function is placed, so this gives you 4
So although you have to enter it as an array formula (you will get an N/A error without the ctrl+shift+enter), the result is just a single number.
Also, depending on your data set, you might want to add some ERROR handling in case no match has been found, e.g. just using the example data set in your question, the result in cell D5 will be N/A so you have to decide what value you want the result to be in such case.
And finally, I did not use the values in column A, as I assumed this is just a sequential ascending counter. If this is not the case, and you specifically want to find the difference between the corresponding values in that column, you can use the variant mentioned by Foxfire... in one of the other answers: =MIN(IF(B2:$B$6>C2;A2:$A$6))-A2
A slightly adjusted and shortened answer on Peter K.'s suggestion:
In D2:
=MATCH(TRUE,$B3:B$7>C2,0)
enter the formula with ctrl+shift+enter
Something like this should work for you. First transform your range to a table.
=IFERROR(AGGREGATE(15,6,--([#Second]<[First])*(ROW([#Second])<=ROW([First]))/--([#Second]<[First]*(ROW([#Second])<=ROW([First])))*[Count],1) - [#Count],"")
In this formula the comparison between [second] and [First] starts at the same row. That means that the value in D5 is 0 and not 1. (Like #Foxfire And Burns And Burnslike stated in the comments).
Ok, I did not post the answer waiting until OP answered why D5 is 1 instead of 0, but my formula is also an array formula. It would be:
=MIN(IF(B2:$B$6>C2;A2:$A$6))-A2
To type this formula in array mode, you need to type it as usual, but instead of pressing ENTER, you need to press CTRL+SHIFT+ENTER

IF Function/VLOOKUP Combo

So this seems like it should be pretty easy. I could just concatenate and make another column in the data to make a unique combo and get my answer. But that just seems so messy. So here I am reaching out to you fine folks to pick your brains.
I want to look up HQLine and Description in the MPCC tab to return the correct MPCC Code. I tried a couple IF statements with VLOOKUPS but couldn't get it right.
So I need to look up BK3 Positive Crankcase Ventilation (PCV) Connector in the MPCC tab. So it needs to match BK3 and the Long description and then give me the correct code.
Here is the missing data file
Here is the MPCC export list that I want to search
Use SUMIFS.
SUMIFS will find the sum in a table of rows that meet certain criteria. If the MPCC is always a number, and the MQAb-LongDescription is always unique, SUMIFS will find the correct ID.
=SUMIFS(Sheet1!C$2:C$100,Sheet1!A$2:A$100,A2,Sheet1!B$2:B$100,B2)
where Sheet1!A$2:A$100 is the HQAb data, Sheet1!B$2:B$100 is the Long Description data, Sheet1!C$2:C$100 is the MPCC Number data, A2 is the HQLine, and B2 is the Description.
The formula would go in C1.
More information on VLookup with Multiple Criteria
You can use an Index/Match with multiple criteria.
I'm assuming that you will put this formula in "Sheet1", cell C2, and your lookup data is in a sheet called "Sheet2", columns A, B, C from row 2 to 30.
Put this in Sheet1, C2:
=INDEX(Sheet2!$C$2:$C$30,MATCH(A2&B2,Sheet2!$A$2:$A$30&Sheet2!$B$2:$B$30,0))
(Enter with CTRL+SHIFT+ENTER) and drag down.)
Adjust the ranges as necessary.
lets assume your first Table is on sheet 1 in the range A1:C11 and the MPCC codes are located on Sheet 2 in the range A1:C32. Each table has a header row so your data really starts in row 2.
Similar to BruceWayne's answer of using an array formula, you can bring the array calculation inside of a formula and avoid the special array treatment. There are a few functions that can do this. I will demonstrate with the SUMPRODUCT function;
On Sheet 1, Cell C2, use the following formula:
=INDEX('Sheet 2'!$C$1:C$32,SUMPRODUCT((A2='Sheet 2'!$A$2:A$32)*(B2='Sheet 2'!$B$2:B$32)*row('Sheet 2'!$A$2:A$32))
Explanation:
When the value in A2 matches the value in the range in the second sheet it will be true and false when it does not. when True False get used in math operations they are treated at 1 and 0 respectively. Therefore the only result from your two search criteria will be the rows where A2 match is true and B2 match is true and this will have a value of 1. The 1 will then be multiplied by the row number. Since all other results will be 0 since your list is a unique combination, the sum part of sumproduct will sum up to the row number where your unique row is located. This in turn is used by the indext function to return the row to give your unique number.

How does the SUMPRODUCT command works in this example?

The following code allows me to determine distinct values in a pivot table in Excel:
=SUMPRODUCT(($A$A:$A2=A2)*($B$2:$B2=B2))
See also: Simple Pivot Table to Count Unique Values
The code runs perfectly fine. However, can somebody help me understand how this code actually works?
You write: the following code allows me to determine distinct values in a pivot table in Excel
No. That formula alone does not do that. Read on for the explanation of what does.
There's a typo in the formula. It should be
=SUMPRODUCT(($A$2:$A2=A2)*($B$2:$B2=B2))
See the difference?
The formula starts in row 2 and is copied down. In each row, the $A$2 reference and the $B$2 reference will stay the same. The $ signs make them absolute references. The relative references $A2 and A2 will change their row numbers when copied down, so in row 3 the A2 will change to A3 and B2 will change to B3. In the next row it will be A4 and B4, and so on.
You may want to create a sample scenario with data similar to that in the thread you link to. Then use the "Evaluate Formula" tool on the Formulas ribbon to see step by step what is calculated. The formula evaluates from the inside out. Let's assume the formula has been copied down to row 5 and we are now looking at
=SUMPRODUCT(($A$2:$A5=A5)*($B$2:$B5=B5))
($A$2:$A5=A5) this bit compares all the cells from A2 to A5 with the value in A5. The result is an array of four values, either true or false. The next bit ($B$2:$B5=B5) also returns an array of true or false values.
These two arrays are multiplied and the result is an array of 1 or 0 values. Each array has the same number of values.
The first value of the first array will be multiplied with the first value of the second array. (see the red arrows)
The second value of the first array will be multiplied with the second value of the second array. (see the blue arrows)
and so on.
True * True will return 1, everything else will return 0. The result of the multiplication is:
The nature of the SumProduct function is to sum the result of the multiplications (the product), so that is what it does.
This function alone does not do anything at all to establish distinct values in Excel. In the thread you link to, the Sumproduct is wrapped in an IF statement and THAT is where the distinct values are identified.
=IF(SUMPRODUCT(($A$2:$A2=A2)*($B$2:$B2=B2))>1,0,1)
In plain words: If the combination of the value in column A of the current row and column B of the current row has already appeared above, return a zero, otherwise, return a 1.
This marks distinct values of the combined columns A and B.
Firts, i think you made a type here, as the formula should be :
=SUMPRODUCT(($A$2:$A2=A2)*($B$2:$B2=B2))
Let's decompose it in 2 parts:
First, we check the cells between A2 and A2, so only one cell, and we check the number of cells wich are equals to A2. In this case, the output should be 1, as you're comparing A2 with A2. However, you're not limited to compare A2 with A2. If you had chosen 2 cells equals to A2, the results would have been 2.You can compare as many cells as you want with A2 (replace the characters after the $ to modulate).
We do the same for the second bracket, except the pivot value is B2.
After that, you need to understand what the function SUMPRODUCT does. It sum the value of the product for a range of array. For example, say you have the value 1 on A1, 1 on A2, 2 on B1 and 3 on B2, if you make SUMPRODUCT((A1:A2)*(B1:B2)) , you will obtain (1*2) + (1*3) = 5. So, in the example you gave us, it will give the sum of (A2=A2)*(B2=B2) = 1.
So, it will output the number of pair (Ax,Bx) which is equals to (A2,B2). With the link, you can see that, if you select the first line only, the function will output 1 (and so the IF will output 1), but if you select the first 2 lines, the function will output 2, (and so the IF will output 0).
I hope this made sense to you, as i hoped i didn't make any mistakes along the explanation.

How to look up value in a column, and assign specific entries

I have a sheet in Excel where the columns contain different names of people that are in different teams in my department (i.e. Names in Column 1 are in Team 1).
For another sheet, I need to have a formula that achieves the following:If I write a name in Cell B2 that can be found in the first column of that other sheet (Team 1), Excel should populate cell B6 with "Team 1".
If instead, the name I wrote is found in the second column, then the text should read "Team 2".
I've tried a couple of different formulas w/o success, and stringing a lot of IF and OR functions together is way too cumbersome. Anybody has a better solution?
If you need this formula for more then only two column then id use the formula
=INDEX(Sheet1!C1:G1,SUMPRODUCT((Sheet1!C2:G6=B1)*COLUMN(Sheet1!C2:G6)))
Say you have a set-up like:
And in B1 of Sheet2 you enter Name3, you want it to return TEAM1 as shown below:
The only catch to this formula is that you will need to tell it how many columns are before your data so if you have a table more like:
Then you need to tell the formula that there are 2 rows before your data starts, your new formula will be
=INDEX(Sheet1!C1:G1,SUMPRODUCT((Sheet1!C2:G6=B1)*COLUMN(Sheet1!C2:G6))-2)
Notice the -2 before the last parenthesis this is to indicate that there are 2 columns BEFORE you data.
and the new result for looking up say NAME20 would become as follows:
*EXPLANATION: *
The formula works as follows , I will do it all on one page for easier viewing and instead of Sheet2!B2 and B6 I will use G2, and G6 on the same sheet as the data.
First we look at all values in the Range A1:E6 and find the one with the matching name so we use
=A1:E6=G2
Now when you enter this into the cell you will recieve a #VALUE,but if you goto the Formula Bar and press F9 it will shwo you the full value of the formula
{FALSE,FALSE,FALSE,FALSE,FALSE;FALSE,FALSE,FALSE,FALSE,FALSE;FALSE,FALSE,FALSE,FALSE,FALSE;FALSE,FALSE,TRUE,FALSE,FALSE;FALSE,FALSE,FALSE,FALSE,FALSE;FALSE,FALSE,FALSE,FALSE,FALSE}
In this we know that one cell contains the Name we searched for, this is represented by the TRUE
Now when using a formula like SUMPRODUCT, FALSE = 0 and TRUE = 1
So to get the column we mutiply every result by the column they are in.
=SUMPRODUCT((A1:E6=G2)*COLUMN(A1:E6))
So any cells that don't =G2 are FAlSE or 0, and when you multiply the column of that cell by 0 (because false = 0) it will simply result in 0. But for the one cell that IS TRUE or 1 When you multiply the column of that cell by 1 (because TRUE = 1) The product will be the cells column number
So as shown in the picture when looking for NAME13 the result of the sumproduct is 3 (its column)
Then to return the TEAM for that name, we use the INDEX function will return the value in a cell at the given cordinates.
In this case we want the value from A1:E1 that is in the same column as the Name we matched. When looked at as an Array A1:E1 looks like
{"TEAM1","TEAM2","TEAM3","TEAM4","TEAM5"}
And we want to return the value in that array at the same position as the column number of our cell, Minus any nonincluded column before the data, thus the reason for the -2 in the secon example I gave you.
So:
=INDEX(A1:E1,SUMPRODUCT((A1:E6=G2)*COLUMN(A1:E6)))
In this example excel interprets this formula as
Return the 3rd value (the 3 comes from the sumproduct as explained earlier) in the list of value from A1:E1. And that value would be TEAM3.
use a MATCH() or VLOOKUP() function inside an IF(). SOmthing like:
IF(NOT(ISERROR(MATCH(...)));"Team1";"")
Did you know you can browse functions by catergoty (such as Search), just clikc the fx left of your formula bar and use the drop down box. Each formula comes with a description with usually rather clear information...

Resources