Add many IFs to a formula - excel

Is there an easier and more compact way to write this formula?
[1] [2]
A 0
B 5940
C 13860
D 22500
E 87300
F 378000
=IF(Q27="A",0,IF(Q27="B",5940,IF(Q27="C",13860,IF(Q27="D",22500,IF(Q27="E",87300,IF(Q27="F",378000,"ERROR"))))))
So what it does is return the respective values in [2] if Q27 equals one of the letters in [1].
I managed to get the result I expected but I'd like to know if the code can be improved.
Thanks

That is what VLOOKUP is for. Put your table in A1:B6 then:
=VLOOKUP(Q27,A:B,2,FALSE)
Or you can "Hard Code" the VLOOKUP like this:
=VLOOKUP(Q27,{"A",0;"B",5940;"C",13860;"D",22500;"E",87300;"F",378000},2,FALSE)
Or:
=INDEX({0,5940,13860,22500,87300,378000},MATCH(Q27,{"A","B","C","D","E","F"},0))

If you have to hard-code the values then maybe,
=iferror(choose(code(upper(q27))-64, 0, 5940, 13860, 22500, 87300, 378000), "error")

The following formula:
=INDEX($B$2:$B$7,MATCH(E2,$A$2:$A$7))
Enter the value you're using to look up in cell E2. If you were to put the formula in cell Q27 as you specified, then you would get the return value in that cell. If you were to put D into cell E2, and have the formula in cell Q27, you would get 22500 in Q27.
Alternatively, if the helper cell E2 isn't an option, you could simply replace E2 in the formula with "A", or "B", or whichever specific value you're using to lookup. Be sure to include quotation marks around it. For example:
=INDEX($B$2:$B$7,MATCH("A",$A$2:$A$7))

Related

Excel - SUMIFS over a set

I am trying to use a sumif where the criteria is that column B equals 1 or 11:
=SUMIFS(A:A,B:B,{1,11})
The formula above works perfectly. But now what I would like to do is store {1,11} in a cell (e.g. cell C1). Then:
=SUMIFS(A:A,B:B,C1)
This no longer works, and it seems to pull up C1 as "{1,11}". Even though there are no visible quotes in cell C1, when I highlight C1 and press F9 in the sumif formula, quotes do come up.
Is there any way to do this? I want to be able to set the array {1,11} as a variable in a separate cell, because this could change. I do not want it hardcoded within the sumifs formula.
Thanks!!
Place your desired lookup in C1 and down, this is dynamic
=SUMPRODUCT(SUMIFS(A:A,B:B,C1:INDEX(C:C,MATCH(1E+99,C:C))))
Just make sure there are no other numbers in column C and the list is contiguous.
One possibility is you can use text in C1:
'{1,11}
And then define a name to convert it to an array:
EvalC1 refers to
=EVALUATE($C$1)
Note you can use the name in your array formula:
=SUMIFS(A:A,B:B,EvalC1)

Excel Formula with IF... ELSE

Hi all,
I have this excel where by I need to find the location of the item if they are found in column B.
So In my F column, I tried to write ifelse formula which didnt work.which is
=IF(D2="NULL","NONE",C((D2))).
My idea is if D2 is not null, use the value in D column to find the location in C column. In this example, fish no 4, so it is found, my F column should show the value "C" using the value shown in D column and use it as Row no in C column
I hope you guys get the idea and help me out a newbie in excel. Thanks in advance
=vlookup($D2,$A$2:$C$6,3,0)
you can use that in column F. Place that formula in F2 and copy down.
you could technically use it in column E as well, but you would need to change the 3 to a 2.
you did not say what you wanted to do if the D value was "Null" so I am going to take a stab at the dark and wrap you lookup formula in an if statement that will deal with "Null" or empty cells
=IF(OR($D2="NULL",$D2=""),"",VLOOKUP($D2,$A$2:$C$6,3,0))
That is the alternative formula to place in F2 and copy down.
Use the formula:
=IF(D2<>"NULL",VLOOKUP(D2,A2:C6,3,FALSE),"Value is NULL")
Here is the working example:
Put formula in cell F2 and drag it down.
[edit]to pull proper location column, not just the row #[/edit]
Seems like a job for MATCH+OFFSET
Try this formula in cell F2:
=OFFSET($C$1, MATCH(E2,B:B,0)-1, 0, 1, 1)
Match is used to locate the value in the first argument (ie E2) within the range specified in 2nd argument (ie B:B). I use B:B but you could also use range B2:B30 or whatever more specific range you want. (I prefer the more generic B:B, though :) )
Third paramter "0" just indicates "Exact match".
This function will retun "#N/A" if nothing found.
OFFSET takes the result from MATCH to pick out the Location you want. The first parameter in OFFSET is the rows below (or above if negative) from the base row (in this case $C$1). the next is the column: 0 since we're in the column we want to be in. The last two are the size of the range: 1,1 is a 1x1 cell, so just 1 cell. If we did ...,2,3), that would be 2 rows high and 3 columns wide - or a 6 cell range. We're just after 1 cell here.
I've always preferred MATCH + OFFSET to other options, I just found they held up more robustly to changes in a sheet (ie new rows/columns added). So it's mostly personaly preference over VLOOKUP and INDEX. I honestly have never compared their actual performance, however, I've never had any issues with MATCH+OFFSET running slowly :)

Excel If Then Formula

I'm currently have trouble with a formula. Essentially, I want a formula that will: 1) Check Column D to see if the returned value is "Yes". 2) If Yes, proceed to execute a LOOKUP formula. Here is the formula I have so far--unfortunately it's not working. Any help would be much appreciated!
=IF(D19<>YES, VLOOKUP(C19,'Workbench Exported List'!A2:A961, 'Workbench Exported List'!B2:B961), "Not Available")
A few issues with your formula as written:
Given that you want the VLOOKUP formula if col D = "Yes", your formula needs to be revised to use = rather than <>.
Text values (such as Yes) need to be enclosed in quotation marks when used in a formula ("Yes").
Your VLOOKUP() syntax is incorrect. You need to use VLOOKUP(value, array, column-number, TRUE/FALSE).
Without seeing your data, I'm assuming you want to look up the value in C19 in col A and return the value from col B. Try: =IF(D19="Yes", VLOOKUP(C19, 'Workbench Exported List'!A2:B961, 2, FALSE), "Not Available").
This will search for an exact match of the value in C19 in column 2 of 'Workbench Exported List'!A2:B961 (col B) if D19 is equal to Yes. If D19 is not equal to Yes, the formula will return Not Available.

EXCEL How do I ignore a cell if a cell has a value

I have a row that will have weekly values entered. Column B has the initial value, and E has the calculation; as I add values to C, D and so on, I want the calculation to skip the previous columns value when the next column gets a value.
B1-C1=E1 BUT when a value is added to D1, E1 would update to B1-D1=E1
Sorry for the horrible description. This is probably answered somewhere on this site but I am not sure what terms to search.
Many thanks!
you can use an if statement. I am not 100% sure of your problem but something like this might be helpful.
if(A1, A1, 0)
So for your example provided.
=B1-if(D1, D1, C1)
This says if there is a value in D1 use D1 else use C1. This works in this example, because if
D1 is empty or 0 you will use the other cell. This may change for any given problem.
Use this if function in E1:
=IF(D1>0,B1-D1,IF(C1>0,B1-C1,B1))
Then enter a value in B1, then C1 then D1 to see the results.
According to your question, you only have room for two entries after the default B1 value. This statement will handle that.
If you need more fields, nest more if functions. But if's can only be nested 7 deep, so you can only have an initial value in B1 and 7 more cells, C1 to I1 with your formula in J1
If you actual data is as simple as your sample data you could just use:
=IF(LEN(D2)>0, B2-D2,B2-C2)
You could also use:
=IF(ISBLANK(D2), B2-C2, B2-D2)
if you prefer but Len is a little shorter and I believe the ISBLANK() function has flaws, If you have a formula in D2 that has a calculation and you set the result to "" then it will pick up as false. It depends on your needs.
I would do the following.
In E1 paste the following:
=A1-INDEX(B1:D1,1,COUNT(B1:D1))
The count formula will tell how many values are present in the range of B:D column. This will be used to catch the last column with an index formula which will be deducted form A1.
One thing is very important! The values from A to D has to be written in sequence, if one column is missing than the calculation will be false.
Hope I could help!

Excel INDEX and MATCH Get Value

I have an excel workbook that I need some help with INDEX and MATCH or any other Formula that can get me my end result.
Here is sheet1:
SIT_ID METER SUSE_CD
10834282 DT0061 B
10834282 AW7931 P
21676286 CQ9635 P
21676286 DP4838 B
21726281 AW7880 P
21726281 DT0032 B
Here is Sheet2:
Site ID B P
10834282
21676286
21726281
Ultimately what I am trying to do is on Sheet2 is put the Meter that = B for the SITEID in the column and then Put the Meter that = P in the Same row.
I have never used Index or Match and I looked it up online but I am confused and hoping someone can help me with the correct formula or point me in the right direction.
Thanks so much!
INDEX first takes a range, then a row number, an optional column number (and an optional area number).
MATCH takes a value to lookup, an array and a mode.
In your problem you can use the following in Sheet2 cell B2:
=INDEX(Sheet1!$B$2:$B$7, MATCH($A2, IF(Sheet1!$C$2:$C$7=B$1,Sheet1!$A$2:$A$7), 0))
This formula is an array formula and will work with Ctrl+Shift+Enter and then you can fill it to the other cells.
I had to use an IF because there're two conditions to check.
EDIT: Use this one if your cell formats are different:
=INDEX(Sheet1!$B$2:$B$7,MATCH($A2*1,IF(Sheet1!$C$2:$C$7=B$1,Sheet1!$A$2:$A$7*1),0))
EDIT2: Adding trimming:
=INDEX(Sheet1!$B$2:$B$7,MATCH($A2*1,IF(TRIM(Sheet1!$C$2:$C$7)=TRIM(B$1),Sheet1!$A$2:$A$7*1),0))
EDIT3: If you're using it on your full data, change the range:
=INDEX(Sheet1!$B:$B,MATCH($A2*1,IF(TRIM(Sheet1!$C:$C)=TRIM(B$1),Sheet1!$A:$A*1),0))
Assuming your Sheet1 looks like this:
And your Sheet2 looks like this:
The formula in Sheet2 cell B2 and copied over and down to cell C4 is:
=INDEX(Sheet1!$B$2:$B$7,MATCH(1,INDEX((Sheet1!$A$2:$A$7=$A2)*(Sheet1!$C$2:$C$7=B$1),),0))
Note that this is a regular formula, so no need for Ctrl+Shift+Enter
A helper column D is added to initial columns.
D2: =$A2 & $C2
Now it's possible to make a simple search of the concatenated SITE_ID and SUSE_CD:
H2: =MATCH($G2&" B";$D$2:$D$8;0)
The result would be a row number (=1 in this case) for the needed string in array $D$2:$D$8.
INDEX shows the value of the cell, found by counting n-th row (defined by MATCH) and m-th column (=2) in array $A2:$A$8 from the upper left cell (A2).
Altogether: =INDEX($A$2:$B$8;MATCH($G2&" B";$D$2:$D$8;0);2)
The easiest way to get around with this is,
to use concatenation operator in the match function.
Don't forget to use Ctrl+Shift+Enter
Use below formula in column B of Sheet 2
{=INDEX(Sheet1!$B:$B,MATCH(Sheet2!$A2&Sheet2!$B$1,Sheet1!$A:$A&Sheet1!$C:$C,0))}
And the below formula in column C of Sheet 2
{=INDEX(Sheet1!$B:$B,MATCH(Sheet2!$A2&Sheet2!$C$1,Sheet1!$A:$A&Sheet1!$C:$C,0))}
And then flash fill the remaining rows.

Resources