I wrote a formula which exceeds 8192 characters and I really need to reach the result of the formula. Actually it is repetetive calculation for different conditions. I'm sure when you see the formula you will get the idea. But there is short explanation below:
There are 10 different sources and regarding choosen source formula should work.
Regarding choosen currency I need to get result of related calculation.
Right now it is 10.290 chars.
Since this is my first question and I'm kinda new on Excel any help would be appreciated.
*Expected result need to be shown on "unit price" column and different for each item.
=IFERROR(IF(core!$D$11=1;IF($F15>0;IF(core!$D$7="TL";IF(core!$H29="TL";core!$I29+core!$I29*core!$F29+core!$I29*core!$g29;IF(core!$H29="USD";core!$I29*core!$N$2+core!$I29*core!$N$2*core!$F29+core!$I29*core!$N$2*core!$g29;IF(core!$H29="EURO";core!$I29*core!$N$3+core!$I29*core!$N$3*core!$F29+core!$I29*core!$N$3*core!$g29;"")));
IF(core!$D$11=1;IF($F15>0;IF(core!$D$7="USD";IF(core!$H29="TL";core!$I29/core!$N$2+core!$I29/core!$N$2*core!$F29+core!$I29/core!$N$2*core!$g29;IF(core!$H29="USD";core!$I29+core!$I29*core!$F29+core!$I29*core!$g29;IF(core!$H29="EURO";core!$I29*core!$N$3/core!$N$2+core!$I29*core!$N$3/core!$N$2*core!$F29+core!$I29*core!$N$3/core!$N$2*core!$g29;"")));
IF(core!$D$11=1;IF($F15>0;IF(core!$D$7="EURO";IF(core!$H29="TL";(core!$I29/core!$N$3+core!$I29/core!$N$3*core!$F29+core!$I29/core!$N$3*core!$g29);IF(core!$H29="USD";(core!$I29*core!$N$2/core!$N$3+core!$I29*core!$N$2/core!$N$3*core!$F29+core!$I29*core!$N$2/core!$N$3*core!$g29);IF(core!$H29="EURO";core!$I29+core!$I29*core!$F29+core!$I29*core!$g29;""))))))))))));
IF(core!$D$11=2;IF($F15>0;IF(core!$D$7="TL";IF(core!$H29="TL";core!$K29+core!$K29*core!$F29+core!$K29*core!$g29;IF(core!$H29="USD";core!$K29*core!$N$2+core!$K29*core!$N$2*core!$F29+core!$K29*core!$N$2*core!$g29;IF(core!$H29="EURO";core!$K29*core!$N$3+core!$K29*core!$N$3*core!$F29+core!$K29*core!$N$3*core!$g29;"")));
IF(core!$D$11=2;IF($F15>0;IF(core!$D$7="USD";IF(core!$H29="TL";core!$K29/core!$N$2+core!$K29/core!$N$2*core!$F29+core!$K29/core!$N$2*core!$g29;IF(core!$H29="USD";core!$K29+core!$K29*core!$F29+core!$K29*core!$g29;IF(core!$H29="EURO";core!$K29*core!$N$3/core!$N$2+core!$K29*core!$N$3/core!$N$2*core!$F29+core!$K29*core!$N$3/core!$N$2*core!$g29;"")));
IF(core!$D$11=2;IF($F15>0;IF(core!$D$7="EURO";IF(core!$H29="TL";core!$K29/core!$N$3+core!$K29/core!$N$3*core!$F29+core!$K29/core!$N$3*core!$g29;IF(core!$H29="USD";core!$K29*core!$N$2/core!$N$3+core!$K29*core!$N$2/core!$N$3*core!$F$29+core!$K29*core!$N$2/core!$N$3*core!$g$29;IF(core!$H29="EURO";core!$K29+core!$K29*core!$F29+core!$K29*core!$g29;"")))))))))));
IF(core!$D$11=3;IF($F15>0;
IF(core!$D$7="TL";IF(core!$H29="TL";core!$M29+core!$M29*core!$F29+core!$M29*core!$g29;IF(core!$H29="USD";core!$M29*core!$N$2+core!$M29*core!$N$2*core!$F29+core!$M29*core!$N$2*core!$g29;IF(core!$H29="EURO";core!$M29*core!$N$3+core!$M29*core!$N$3*core!$F29+core!$M29*core!$N$3*core!$g29;"")));
IF(core!$D$11=3;IF($F15>0;IF(core!$D$7="USD";IF(core!$H29="TL";core!$M29/core!$N$2+core!$M29/core!$N$2*core!$F29+core!$M29/core!$N$2*core!$g29;IF(core!$H29="USD";core!$M29+core!$M29*core!$F29+core!$M29*core!$g29;IF(core!$H29="EURO";core!$M29*core!$N$3/core!$N$2+core!$M29*core!$N$3/core!$N$2*core!$F29+core!$M29*core!$N$3/core!$N$2*core!$g29;"")));
IF(core!$D$11=3;IF($F15>0;IF(core!$D$7="EURO";IF(core!$H29="TL";core!$M29/core!$N$3+core!$M29/core!$N$3*core!$F29+core!$M29/core!$N$3*core!$g29;IF(core!$H29="USD";core!$M29*core!$N$2/core!$N$3+core!$M29*core!$N$2/core!$N$3*core!$F29+core!$M29*core!$N$2/core!$N$3*core!$g29;IF(core!$H29="EURO";core!$M29+core!$M29*core!$F29+core!$M29*core!$g29;"")))))))))));
IF(core!$D$11=4;IF($F15>0;
IF(core!$D$7="TL";IF(core!$H29="TL";core!$O29+core!$O29*core!$F29+core!$O29*core!$g29;IF(core!$H29="USD";core!$O29*core!$N$2+core!$O29*core!$N$2*core!$F29+core!$O29*core!$N$2*core!$g29;IF(core!$H29="EURO";core!$O29*core!$N$3+core!$O29*core!$N$3*core!$F29+core!$O29*core!$N$3*core!$g29;"")));
IF(core!$D$11=4;IF($F15>0;IF(core!$D$7="USD";IF(core!$H29="TL";core!$O29/core!$N$2+core!$O29/core!$N$2*core!$F29+core!$O29/core!$N$2*core!$g29;IF(core!$H29="USD";core!$O29+core!$O29*core!$F29+core!$O29*core!$g29;IF(core!$H29="EURO";core!$O29*core!$N$3/core!$N$2+core!$O29*core!$N$3/core!$N$2*core!$F29+core!$O29*core!$N$3/core!$N$2*core!$g29;"")));
IF(core!$D$11=4;IF($F15>0;IF(core!$D$7="EURO";IF(core!$H29="TL";core!$O29/core!$N$3+core!$O29/core!$N$3*core!$F29+core!$O29/core!$N$3*core!$g29;IF(core!$H29="USD";core!$O29*core!$N$2/core!$N$3+core!$O29*core!$N$2/core!$N$3*core!$F29+core!$O29*core!$N$2/core!$N$3*core!$g29;IF(core!$H29="EURO";core!$O29+core!$O29*core!$F29+core!$O29*core!$g29;"")))))))))));
IF(core!$D$11=5;IF($F15>0;
IF(core!$D$7="TL";IF(core!$H29="TL";core!$Q29+core!$Q29*core!$F29+core!$Q29*core!$g29;IF(core!$H29="USD";core!$Q29*core!$N$2+core!$Q29*core!$N$2*core!$F29+core!$Q29*core!$N$2*core!$g29;IF(core!$H29="EURO";core!$Q29*core!$N$3+core!$Q29*core!$N$3*core!$F29+core!$Q29*core!$N$3*core!$g29;"")));
IF(core!$D$11=5;IF($F15>0;IF(core!$D$7="USD";IF(core!$H29="TL";core!$Q29/core!$N$2+core!$Q29/core!$N$2*core!$F29+core!$Q29/core!$N$2*core!$g29;IF(core!$H29="USD";core!$Q29+core!$Q29*core!$F29+core!$Q29*core!$g29;IF(core!$H29="EURO";core!$Q29*core!$N$3/core!$N$2+core!$Q29*core!$N$3/core!$N$2*core!$F29+core!$Q29*core!$N$3/core!$N$2*core!$g29;"")));
IF(core!$D$11=5;IF($F15>0;IF(core!$D$7="EURO";IF(core!$H29="TL";core!$Q29/core!$N$3+core!$Q29/core!$N$3*core!$F29+core!$Q29/core!$N$3*core!$g29;IF(core!$H29="USD";core!$Q29*core!$N$2/core!$N$3+core!$Q29*core!$N$2/core!$N$3*core!$F29+core!$Q29*core!$N$2/core!$N$3*core!$g29;IF(core!$H29="EURO";core!$Q29+core!$Q29*core!$F29+core!$Q29*core!$g29;"")))))))))));
IF(core!$D$11=6;IF($F15>0;
IF(core!$D$7="TL";IF(core!$H29="TL";core!$S29+core!$S29*core!$F29+core!$S29*core!$g29;IF(core!$H29="USD";core!$S29*core!$N$2+core!$S29*core!$N$2*core!$F29+core!$S29*core!$N$2*core!$g29;IF(core!$H29="EURO";core!$S29*core!$N$3+core!$S29*core!$N$3*core!$F29+core!$S29*core!$N$3*core!$g29;"")));
IF(core!$D$11=6;IF($F15>0;IF(core!$D$7="USD";IF(core!$H29="TL";core!$S29/core!$N$2+core!$S29/core!$N$2*core!$F29+core!$S29/core!$N$2*core!$g29;IF(core!$H29="USD";core!$S29+core!$S29*core!$F29+core!$S29*core!$g29;IF(core!$H29="EURO";core!$S29*core!$N$3/core!$N$2+core!$S29*core!$N$3/core!$N$2*core!$F29+core!$S29*core!$N$3/core!$N$2*core!$g29;"")));
IF(core!$D$11=6;IF($F15>0;IF(core!$D$7="EURO";IF(core!$H29="TL";core!$S29/core!$N$3+core!$S29/core!$N$3*core!$F29+core!$S29/core!$N$3*core!$g29;IF(core!$H29="USD";core!$S29*core!$N$2/core!$N$3+core!$S29*core!$N$2/core!$N$3*core!$F29+core!$S29*core!$N$2/core!$N$3*core!$g29;IF(core!$H29="EURO";core!$S29+core!$S29*core!$F29+core!$S29*core!$g29;"")))))))))));
IF(core!$D$11=7;IF($F15>0;
IF(core!$D$7="TL";IF(core!$H29="TL";core!$U29+core!$U29*core!$F29+core!$U29*core!$g29;IF(core!$H29="USD";core!$U29*core!$N$2+core!$U29*core!$N$2*core!$F29+core!$U29*core!$N$2*core!$g29;IF(core!$H29="EURO";core!$U29*core!$N$3+core!$U29*core!$N$3*core!$F29+core!$U29*core!$N$3*core!$g29;"")));
IF(core!$D$11=7;IF($F15>0;IF(core!$D$7="USD";IF(core!$H29="TL";core!$U29/core!$N$2+core!$U29/core!$N$2*core!$F29+core!$U29/core!$N$2*core!$g29;IF(core!$H29="USD";core!$U29+core!$U29*core!$F29+core!$U29*core!$g29;IF(core!$H29="EURO";core!$U29*core!$N$3/core!$N$2+core!$U29*core!$N$3/core!$N$2*core!$F29+core!$U29*core!$N$3/core!$N$2*core!$g29;"")));
IF(core!$D$11=7;IF($F15>0;IF(core!$D$7="EURO";IF(core!$H29="TL";core!$U29/core!$N$3+core!$U29/core!$N$3*core!$F29+core!$U29/core!$N$3*core!$g29;IF(core!$H29="USD";core!$U29*core!$N$2/core!$N$3+core!$U29*core!$N$2/core!$N$3*core!$F29+core!$U29*core!$N$2/core!$N$3*core!$g29;IF(core!$H29="EURO";core!$U29+core!$U29*core!$F29+core!$U29*core!$g29;"")))))))))));
IF(core!$D$11=8;IF($F15>0;
IF(core!$D$7="TL";IF(core!$H29="TL";core!$W29+core!$W29*core!$F29++core!$W29*core!$g29;IF(core!$H29="USD";core!$W29*core!$N$2+core!$W29*core!$N$2*core!$F29+core!$W29*core!$N$2*core!$g29;IF(core!$H29="EURO";core!$W29*core!$N$3+core!$W29*core!$N$3*core!$F29+core!$W29*core!$N$3*core!$g29;"")));
IF(core!$D$11=8;IF($F15>0;IF(core!$D$7="USD";IF(core!$H29="TL";core!$W29/core!$N$2+core!$W29/core!$N$2*core!$F29+core!$W29/core!$N$2*core!$g29;IF(core!$H29="USD";core!$W29+core!$W29*core!$F29+core!$W29*core!$g29;IF(core!$H29="EURO";core!$W29*core!$N$3/core!$N$2+core!$W29*core!$N$3/core!$N$2*core!$F29+core!$W29*core!$N$3/core!$N$2*core!$g29;"")));
IF(core!$D$11=8;IF($F15>0;IF(core!$D$7="EURO";IF(core!$H29="TL";core!$W29/core!$N$3+core!$W29/core!$N$3*core!$F29+core!$W29/core!$N$3*core!$g29;IF(core!$H29="USD";core!$W29*core!$N$2/core!$N$3+core!$W29*core!$N$2/core!$N$3*core!$F29+core!$W29*core!$N$2/core!$N$3*core!$g29;IF(core!$H29="EURO";core!$W29+core!$W29*core!$F29+core!$W29*core!$g29;"")))))))))));
IF(core!$D$11=9;IF($F15>0;
IF(core!$D$7="TL";IF(core!$H29="TL";core!$Y29+core!$Y29*core!$F29+core!$Y29*core!$g29;IF(core!$H29="USD";core!$Y29*core!$N$2+core!$Y29*core!$N$2*core!$F29+core!$Y29*core!$N$2*core!$g29;IF(core!$H29="EURO";core!$Y29*core!$N$3+core!$Y29*core!$N$3*core!$F29+core!$Y29*core!$N$3*core!$g29;"")));
IF(core!$D$11=9;IF($F15>0;IF(core!$D$7="USD";IF(core!$H29="TL";core!$Y29/core!$N$2+core!$Y29/core!$N$2*core!$F29+core!$Y29/core!$N$2*core!$g29;IF(core!$H29="USD";core!$Y29+core!$Y29*core!$F29+core!$Y29*core!$g29;IF(core!$H29="EURO";core!$Y29*core!$N$3/core!$N$2+core!$Y29*core!$N$3/core!$N$2*core!$F29+core!$Y29*core!$N$3/core!$N$2*core!$g29;"")));
IF(core!$D$11=9;IF($F15>0;IF(core!$D$7="EURO";IF(core!$H29="TL";core!$Y29/core!$N$3+core!$Y29/core!$N$3*core!$F29+core!$Y29/core!$N$3*core!$g29;IF(core!$H29="USD";core!$Y29*core!$N$2/core!$N$3+core!$Y29*core!$N$2/core!$N$3*core!$F29+core!$Y29*core!$N$2/core!$N$3*core!$g29;IF(core!$H29="EURO";core!$Y29+core!$Y29*core!$F29+core!$Y29*core!$g29;"")))))))))));
IF(core!$D$11=10;IF($F15>0;
IF(core!$D$7="TL";IF(core!$H29="TL";core!$AA29+core!$AA29*core!$F29+core!$AA29*core!$g29;IF(core!$H29="USD";core!$AA29*core!$N$2+core!$AA29*core!$N$2*core!$F29+core!$AA29*core!$N$2*core!$g29;IF(core!$H29="EURO";core!$AA29*core!$N$3+core!$AA29*core!$N$3*core!$F29+core!$AA29*core!$N$3*core!$g29;"")));
IF(core!$D$11=10;IF($F15>0;IF(core!$D$7="USD";IF(core!$H29="TL";core!$AA29/core!$N$2+core!$AA29/core!$N$2*core!$F29+core!$AA29/core!$N$2*core!$g29;IF(core!$H29="USD";core!$AA29+core!$AA29*core!$F29+core!$AA29*core!$g29;IF(core!$H29="EURO";core!$AA29*core!$N$3/core!$N$2+core!$AA29*core!$N$3/core!$N$2*core!$F29+core!$AA29*core!$N$3/core!$N$2*core!$g29;"")));
IF(core!$D$11=10;IF($F15>0;IF(core!$D$7="EURO";IF(core!$H29="TL";core!$AA29/core!$N$3+core!$AA29/core!$N$3*core!$F29+core!$AA29/core!$N$3*core!$g29;IF(core!$H29="USD";core!$AA29*core!$N$2/core!$N$3+core!$AA29*core!$N$2/core!$N$3*core!$F29+core!$AA29*core!$N$2/core!$N$3*core!$g29;IF(core!$H29="EURO";core!$AA29+core!$AA29*core!$F29+core!$AA29*core!$g29;""))))))))))))))))))))
*(core!$G29+1))
enter image description here
The IFERROR has the format IFERROR(Value, Value_if_error). Your Value is this:
IF(core!$D$11=1;IF($F15>0;IF(core!$D$7="TL";IF(core!$H29="TL";core!$I29+core!$I29*core!$F29+core!$I29*core!$g29;IF(core!$H29="USD";core!$I29*core!$N$2+core!$I29*core!$N$2*core!$F29+core!$I29*core!$N$2*core!$g29;IF(core!$H29="EURO";core!$I29*core!$N$3+core!$I29*core!$N$3*core!$F29+core!$I29*core!$N$3*core!$g29;"")));IF(core!$D$11=1;IF($F15>0;IF(core!$D$7="USD";IF(core!$H29="TL";core!$I29/core!$N$2+core!$I29/core!$N$2*core!$F29+core!$I29/core!$N$2*core!$g29;IF(core!$H29="USD";core!$I29+core!$I29*core!$F29+core!$I29*core!$g29;IF(core!$H29="EURO";core!$I29*core!$N$3/core!$N$2+core!$I29*core!$N$3/core!$N$2*core!$F29+core!$I29*core!$N$3/core!$N$2*core!$g29;"")));IF(core!$D$11=1;IF($F15>0;IF(core!$D$7="EURO";IF(core!$H29="TL";(core!$I29/core!$N$3+core!$I29/core!$N$3*core!$F29+core!$I29/core!$N$3*core!$g29);IF(core!$H29="USD";(core!$I29*core!$N$2/core!$N$3+core!$I29*core!$N$2/core!$N$3*core!$F29+core!$I29*core!$N$2/core!$N$3*core!$g29);IF(core!$H29="EURO";core!$I29+core!$I29*core!$F29+core!$I29*core!$g29;""))))))))))));
Taking your formula, splitting out the first part of the IFERROR, and eliminating the spurious code (i.e. IF(A=1, IF(B=2, IF(A=1, ALWAYS_TRUE, ALWAYS_FALSE), C), D) is functionally identical to IF(A=1, IF(B=2, ALWAYS_TRUE, C), D)), I then built the following table:
The Red, Blue and Green text are Common Terms, present in every formula.
Examining this, and rearranging it slightly, I spot the following patterns:
When D7 is "USD", Divide by $N$2
When D7 is "EURO", Divide by $N$3
When H29 is "USD", Multiply by $N$2
When H29 is "EURO", Multiply by $N$3
Implicitly, the following rules also exist:
When D7 is "TL", Divide by 1
When H29 is "TL", Multiply by 1
I can use a VLOOKUP to pick which cells to use, resulting in the form VLOOKUP(H29,..) * (Common_Terms) / VLOOKUP(D7,..)
This allows me to rewrite your Value as follows:
IF(AND(core!$D$11=1,$F15>0), IF(AND(MATCH(core!$D$11,{"TL","USD","EURO","*"},0)<4, MATCH(core!$H$29,{"TL","USD","EURO","*"},0)<4), VLOOKUP(core!H$29,{"USD",core!$N$2;"Euro",core!$N$3;"TL",1},2,FALSE)*(core!$I29 + core!$I29*core!$F29 + core!$I29*core!$g29)/VLOOKUP(core!H$29,{"USD",core!$N$2;"Euro",core!$N$3;"TL",1},2,FALSE), ""), FALSE)
Which is a third of the length. You should be able to manage similar optimisation of almost every IF condition you have.
More importantly, a lot of your repeated (and unnecessary) IF conditions are checking if H29 and D7 are "TL"/"USD"/"EUR", or checking that F15>0. You can eliminate these by making your outermost IF statement look like this:
=IF(AND($F15>0, MATCH(core!$D$7, core!$D$11, {"TL","USD","EURO","*"},0)<4, MATCH(core!$H$29, {"TL","USD","EURO","*"},0)<4), ALL_THREE_CONTITIONS_ARE_TRUE, "")
In C++ I would use boost::clamp for this. Basically I have some excel function
A1*B2+C3+D4
I want to do constrain it to +/- some number, call it X1. The obvious way is this:
MAX(-X1, MIN(X1, A1*B2+C3+D4))
But I want to be able to do this:
CLAMP(A1*B2+C3+D4, -X1, X1)
Does this or something similar exist? I'm just curious - obviously the workaround works, it's just ugly.
This can be done using MEDIAN. MEDIAN picks the middle of the three values, thus effectively restricting the lower and upper limits.
For example say your minimum is 5 and your maximum is 10:
=MEDIAN(5,0,10) is 5
=MEDIAN(5,7,10) is 7
=MEDIAN(5,12,10) is 10
With spreadsheet formulas I don't think that you can do much better than that min/max construct. You could write clamp in VBA:
Function clamp(x As Double, lower As Double, upper As Double) As Double
If x < lower Then
clamp = lower
ElseIf x > upper Then
clamp = upper
Else
clamp = x
End If
End Function
If you enter that in a standard code module then you could e.g. do something like this:
In column A I have values in the range 0 to 2*pi. In C2 I have the value 0.5. In B1 I entered
=clamp(SIN(A1),-$C$2,$C$2)
and copied down. The graph shows the result.
(Since I gave a VBA solution I'll add the Excel-VBA tag. Even if you prefer a non-VBA solution, it is possible that others in the future might search the question and be comfortable with a VBA solution).
Can anyone tell me what is wrong with this function? Thanks so greatly!!
=IF(B4<7500, "POOR",IF(B4<10000, "GOOD","GREAT",IF(B4>12500,"EXCELLENT"))
Here you go:
=if(B4 < 7500, "POOR", if(B4 < 10000, "GOOD", if(B4 < 12500, "GREAT", "EXCELLENT")))
Note, this assumes that your B4 values are numbers, so you can add error handling if needed.
You could use a lookup function instead of nested IFs. Either store the values in a table in a spreadsheet (easy to maintain) or write the value pairs into the formula:
=LOOKUP(B4,{0,7500,10000,12500},{"poor","good","great","excellent"})
I'm very confused on how to use Microsoft Excel to simulate a "problem", but I've been assured that it's possible.
I have the equation
v(t) = (mg/c)(1-e^((-c/m)(t)))
And I know the values of m, g, k, and c.
m = 170
g = 32 ft/s^2
k = 2.5 lb/ft
c = 1.2 lb/ft/s
So my formula changes into
v(t) = (170*32/1.2)(1-e^((-1.2/170)(t)))
v(t) = (453.33)(1-e^((-.00705)(t)))
The problem is about a bungee jumper, and this is one function that I should use to find velocity, and another that is used for x (distance), but if I can learn how to properly implement this one, I should be able to easily figure out the other one.
I need to somehow implement this in Excel, as a spreadsheet simulation. I have no idea how to implement this in Excel, and I don't know the formulas to do it. I know I could just go through the formula manually and just substitute variables in for t (i.e., .5, 1, 1.5, 2, 2.5, ...), but I know there's supposed to be some way for Excel to do it for me. Additionally, I'm not sure how to simplify the powers and the "e" in my formula, and I actually don't know if I need it if I can just sub in variables like I think I can. Any help would be greatly appreciated.
EDIT: The other state equation, x(t), is below
x(t) = (mg/c)(t) + ((m^2 * g) / c^2))e^((-c/m)(t) - (m^2 * g / c^2))
This formula as mentioned in the OP:
v(t) = (453.33)(1-e^((-.00705)(t)))
needs a little adaptation, as suggested by #Tim Williams, to be suitable for Excel:
=453.33*(1-EXP(-.00705*t)
Excel does not multiply letters by numbers but will attempt to interpret t above as a named range (which may contain one or more numbers) before objecting. So t may be the name given to a range starting .5 and stepping .5 up to and including 10 (may easily be created with Fill , Series…).
If the above formula is then placed in the same row as 0.5 and copied down to suit the results should be as required.
It may however be worth noting that naming a range as a single letter is not best practice and for accuracy, convenience and versatility the constants (eg re gravity) and the variables (eg mass) would be better fed as parameters to the formula.
I have an interesting challenge - I need to run a check on the following data in Excel:
| A - B - C - D |
|------|------|------|------|
| 36 | 0 | 0 | x |
| 0 | 600 | 700 | x |
|___________________________|
You'll have to excuse my wonderfully bad ASCII art. So I need the D column (x) to run a check against the adjacent cells, then convert the values if necessary. Here's the criteria:
If column B is greater than 0, everything works great and I can get coffee. If it doesn't meet that requirement, then I need to convert A1 according to a table - for example, 32 = 1420 and place into D. Unfortunately, there is no relationship between A and what it needs to convert to, so creating a calculation is out of the question.
A case or switch statement would be perfect in this scenario, but I don't think it is a native function in Excel. I also think it would be kind of crazy to chain a bunch of =IF() statements together, which I did about four times before deciding it was a bad idea (story of my life).
Sounds like a job for VLOOKUP!
You can put your 32 -> 1420 type mappings in a couple of columns somewhere, then use the VLOOKUP function to perform the lookup.
Without reference to the original problem (which I suspect is long since solved), I very recently discovered a neat trick that makes the Choose function work exactly like a select case statement without any need to modify data. There's only one catch: only one of your choose conditions can be true at any one time.
The syntax is as follows:
CHOOSE(
(1 * (CONDITION_1)) + (2 * (CONDITION_2)) + ... + (N * (CONDITION_N)),
RESULT_1, RESULT_2, ... , RESULT_N
)
On the assumption that only one of the conditions 1 to N will be true, everything else is 0, meaning the numeric value will correspond to the appropriate result.
If you are not 100% certain that all conditions are mutually exclusive, you might prefer something like:
CHOOSE(
(1 * TEST1) + (2 * TEST2) + (4 * TEST3) + (8 * TEST4) ... (2^N * TESTN)
OUT1, OUT2, , OUT3, , , , OUT4 , , <LOTS OF COMMAS> , OUT5
)
That said, if Excel has an upper limit on the number of arguments a function can take, you'd hit it pretty quickly.
Honestly, can't believe it's taken me years to work it out, but I haven't seen it before, so figured I'd leave it here to help others.
EDIT: Per comment below from #aTrusty:
Silly numbers of commas can be eliminated (and as a result, the choose statement would work for up to 254 cases) by using a formula of the following form:
CHOOSE(
1 + LOG(1 + (2*TEST1) + (4*TEST2) + (8*TEST3) + (16*TEST4),2),
OTHERWISE, RESULT1, RESULT2, RESULT3, RESULT4
)
Note the second argument to the LOG clause, which puts it in base 2 and makes the whole thing work.
Edit: Per David's answer, there's now an actual switch statement if you're lucky enough to be working on office 2016. Aside from difficulty in reading, this also means you get the efficiency of switch, not just the behaviour!
The Switch function is now available, in Excel 2016 / Office 365
SWITCH(expression, value1, result1, [default or value2, result2],…[default or value3, result3])
example:
=SWITCH(A1,0,"FALSE",-1,"TRUE","Maybe")
Microsoft -Office Support
Note: MS has updated that page to only document the behavior of Excel 2019. Eventually, they will probably remove references to 2019 as well... To see what the page looked like in 2016, use the wayback machine:
https://web.archive.org/web/20161010180642/https://support.office.com/en-us/article/SWITCH-function-47ab33c0-28ce-4530-8a45-d532ec4aa25e
Try this;
=IF(B1>=0, B1, OFFSET($X$1, MATCH(B1, $X:$X, Z) - 1, Y)
WHERE
X = The columns you are indexing into
Y = The number of columns to the left (-Y) or right (Y) of the indexed column to get the value you are looking for
Z = 0 if exact-match (if you want to handle errors)
I used this solution to convert single letter color codes into their descriptions:
=CHOOSE(FIND(H5,"GYR"),"Good","OK","Bad")
You basically look up the element you're trying to decode in the array, then use CHOOSE() to pick the associated item. It's a little more compact than building a table for VLOOKUP().
I know it a little late to answer but I think this short video will help you a lot.
http://www.xlninja.com/2012/07/25/excel-choose-function-explained/
Essentially it is using the choose function. He explains it very well in the video so I'll let do it instead of typing 20 pages.
Another video of his explains how to use data validation to populate a drop down which you can select from a limited range.
http://www.xlninja.com/2012/08/13/excel-data-validation-using-dependent-lists/
You could combine the two and use the value in the drop down as your index to the choose function. While he did not show how to combine them, I'm sure you could figure it out as his videos are good. If you have trouble, let me know and I'll update my answer to show you.
I understand that this is a response to an old post-
I like the If() function combined with Index()/Match():
=IF(B2>0,"x",INDEX($H$2:$I$9,MATCH(A2,$H$2:$H$9,0),2))
The if function compare what is in column b and if it is greater than 0, it returns x, if not it uses the array (table of information) identified by the Index() function and selected by Match() to return the value that a corresponds to.
The Index array has the absolute location set $H$2:$I$9 (the dollar signs) so that the place it points to will not change as the formula is copied. The row with the value that you want returned is identified by the Match() function. Match() has the added value of not needing a sorted list to look through that Vlookup() requires. Match() can find the value with a value: 1 less than, 0 exact, -1 greater than. I put a zero in after the absolute Match() array $H$2:$H$9 to find the exact match. For the column that value of the Index() array that one would like returned is entered. I entered a 2 because in my array the return value was in the second column. Below my index array looked like this:
32 1420
36 1650
40 1790
44 1860
55 2010
The value in your 'a' column to search for in the list is in the first column in my example and the corresponding value that is to be return is to the right. The look up/reference table can be on any tab in the work book - or even in another file. -Book2 is the file name, and Sheet2 is the 'other tab' name.
=IF(B2>0,"x",INDEX([Book2]Sheet2!$A$1:$B$8,MATCH(A2,[Book2]Sheet2!$A$1:$A$8,0),2))
If you do not want x return when the value of b is greater than zero delete the x for a 'blank'/null equivalent or maybe put a 0 - not sure what you would want there.
Below is beginning of the function with the x deleted.
=IF(B2>0,"",INDEX...
If you don't have a SWITCH statement in your Excel version (pre-Excel-2016), here's a VBA implementation for it:
Public Function SWITCH(ParamArray args() As Variant) As Variant
Dim i As Integer
Dim val As Variant
Dim tmp As Variant
If ((UBound(args) - LBound(args)) = 0) Or (((UBound(args) - LBound(args)) Mod 2 = 0)) Then
Error 450 'Invalid arguments
Else
val = args(LBound(args))
i = LBound(args) + 1
tmp = args(UBound(args))
While (i < UBound(args))
If val = args(i) Then
tmp = args(i + 1)
End If
i = i + 2
Wend
End If
SWITCH = tmp
End Function
It works exactly like expected, a drop-in replacement for example for Google Spreadsheet's SWITCH function.
Syntax:
=SWITCH(selector; [keyN; valueN;] ... defaultvalue)
where
selector is any expression that is compared to keys
key1, key2, ... are expressions that are compared to the selector
value1, value2, ... are values that are selected if the selector equals to the corresponding key (only)
defaultvalue is used if no key matches the selector
Examples:
=SWITCH("a";"?") returns "?"
=SWITCH("a";"a";"1";"?") returns "1"
=SWITCH("x";"a";"1";"?") returns "?"
=SWITCH("b";"a";"1";"b";TRUE;"?") returns TRUE
=SWITCH(7;7;1;7;2;0) returns 2
=SWITCH("a";"a";"1") returns #VALUE!
To use it, open your Excel, go to Develpment tools tab, click Visual Basic, rightclick on ThisWorkbook, choose Insert, then Module, finally copy the code into the editor. You have to save as a macro-friendly Excel workbook (xlsm).
Even if old, this seems to be a popular questions, so I'll post another solution, which I think is very elegant:
http://fiveminutelessons.com/learn-microsoft-excel/using-multiple-if-statements-excel
It's elegant because it uses just the IF function. Basically, it boils down to this:
if(condition, choose/use a value from the table, if(condition, choose/use another value from the table...
And so on
Works beautifully, even better than HLOOKUP or VLOOOKUP
but... Be warned - there is a limit to the number of nested if statements excel can handle.
Microsoft replace SWITCH, IFS and IFVALUES with CHOOSE only function.
=CHOOSE($L$1,"index_1","Index_2","Index_3")
Recently I unfortunately had to work with Excel 2010 again for a while and I missed the SWITCH function a lot. I came up with the following to try to minimize my pain:
=CHOOSE(SUM((A1={"a";"b";"c"})*ROW(INDIRECT(1&":"&3))),1,2,3)
CTRL+SHIFT+ENTER
where A1 is where your condition lies (it could be a formula, whatever). The good thing is that we just have to provide the condition once (just like SWITCH) and the cases (in this example: a,b,c) and results (in this example: 1,2,3) are ordered, which makes it easy to reason about.
Here is how it works:
Cond={"c1";"c2";...;"cn"} returns a N-vector of TRUE or FALSE (with behaves like 1s and 0s)
ROW(INDIRECT(1&":"&n)) returns a N-vector of ordered numbers: 1;2;3;...;n
The multiplication of both vectors will return lots of zeros and a number (position) where the condition was matched
SUM just transforms this vector with zeros and a position into just a single number, which CHOOSE then can use
If you want to add another condition, just remember to increment the last number inside INDIRECT
If you want an ELSE case, just wrap it inside an IFERROR formula
The formula will not behave properly if you provide the same condition more than once, but I guess nobody would want to do that anyway
If your using Office 2016 or later, or Office 365, there is a new function that acts similarly to a CASE function called IFS. Here's the description of the function from Microsoft's documentation:
The IFS function checks whether one or more conditions are met, and returns a value that corresponds to the first TRUE condition. IFS can take the place of multiple nested IF statements, and is much easier to read with multiple conditions.
An example of usage follows:
=IFS(A2>89,"A",A2>79,"B",A2>69,"C",A2>59,"D",TRUE,"F")
You can even specify a default result:
To specify a default result, enter TRUE for your final logical_test argument. If none of the other conditions are met, the corresponding value will be returned.
The default result feature is included in the example shown above.
You can read more about it on Microsoft's Support Documentation