Conditional IF() statement problem, not returning desired value - excel

Currently writing a program in excel that will return a value based on user input. The current formula has 5 different return options which are returned based on the selection of a number by the user. I use the IF() statement embedded into more IF() statements to account for multiple input options. However, when I go to enter in a number beyond the range of the first IF() statement, I am getting 0 even though it should be a different number.
For the code below, C30 is the input cell and it should return .15 if I was to enter 25.
=IF(C30<20, 0.35, IF(20<C30<40, 0.15, IF(40<C30<60, 0, IF(60<C30<80, -0.1, IF(80<C30, -0.2, 0)))))
From the logic statements, it should be returning .15, but all I am getting is 0.

Excel does not use 20<C30<40 it would be:
AND(20<C30,C30<40)
But you can shorten this with a simple MATCH and CHOOSE:
=CHOOSE(MATCH(C30,{0,20,40,60,80}),0.35,0.15,0,-0.1,-0.2)
If you really want a nested if there is no need for the extra tests:
=IF(C30<20,0.35,IF(C30<40,0.15,IF(C30<60,0,IF(C30<80,-0.1,-0.2))))
IF will resolve sequentially and short circuit as soon as it finds the first TRUE, so it does not need the other logic.

The problem here is the logic that you have used to evaluate whether C30 falls within a range of numbers.
IF(20<C30<40,...) will not check whether C30 is in the range of 20 through 40.
Instead, use AND(cond1, cond2, ...) to check whether the values are within the range:
IF(AND(C30 > 20, C30 < 40), ...)

Replace terms like:
20<C30<40
with:
AND(20<C30,C30<40)
etc.

Related

How do I create the IF Statement in Excel which can handle many conditions. My Example provided

As you can see in my picture.
There is a table to the right.
Cell D3 has the option to select 90.0, 95.0, 99.0, and 99.9
Cell D4 is a % value
Cell D5 I have: IF(AND(D3=90,3/D4<9),3,4) Obviously if the user selects 90 for D3, then I want to determine which 2 X choices either 3 or 4 from the table are used based on the value of Y which I get by the 3/D4. This statement is working 100% correct.
MY QUESTION, How do I add the other parts of the table to this same line of code for when D3 = 95, 99, and 99.9?
I try this: =IF(OR(AND(D3=90, 3/D4<9),3,4, IF(AND(D3=95,4/D4<16),4,5)))
Try
=CHOOSE(MATCH(D3,{90,95,99,99.9},0),
IF(3/D4>= 9, 4,IF(3/D4>= 5,3,NA())),
IF(3/D4>=16, 5,IF(3/D4>= 7,4,NA())),
IF(3/D4>=20, 7,IF(3/D4>= 7,6,IF(3/D4>=6,5,NA()))),
IF(3/D4>=32,10,IF(3/D4>=15,9,IF(3/D4>=9,8,IF(3/D4>=8,7,NA()))))
)
In this formula MATCH(D3,{90,95,99,99.9},0) will return 1, 2, 3, or 4. This is used by CHOOSE to select the login in one of the four lines below.
Each of the next four lines is a nested IF statements. The question as asked has integer thresholds for 3/D4 and is a little ambiguous about things that fall in the gaps: for example, how do you handle C=90 and 3/D4=8.5. I've made the decision/assumption that when 3/D4<9, return 3. If that's wrong (for instance you want to round 3/D4 to an integer) you can modify the formula accordingly.
I've also made the decision to return NA when 3/D4 is below the lowest threshold. You can modify the formula to change that behavior too.

Excel Statement with 4 conditions and 4 answers

All of the methods that I've used have 2 answer values (True or False).
How can I get the following with a formula?
If A1=1 then it's 11, if A1=2 the answer is 22, if A1=3 then it's 33, if A1=4 it's 44.
If the value your are evaluating is in cell A1, then the nested function would be as follows:
IF(A1=1,11,IF(A1=2,22,IF(A1=3,33,IF(A1=4,44,""))))
I put the 2 double commas at the end so the formula returns a blank instead of false.
I don't know if that's what you are asking about, but you can make multiple (nested) IF statements in one. For example:
IF(1=2;TRUE;IF(2=2;TRUE;FALSE))
You just put another IF in the FALSE part of IF statement. If that's not it, can you give a piece of the statement you tried and precise more what do you want?
=IF(AND(INT(A1)=A1,A1<=4,A1>=1),A1*11,"")
Now the above works for the condition you placed in your example, however if one were to go by your title alone you have a couple of options you could go with.
You first Option would be nested IF statements. Like you said each IF function has TRUE or FALSE. The trick is to put another IF function in for the TRUE result and another in for the FALSE results
IF(CHECK1, IF(CHECK2, TRUE2, FALSE2),IF(CHECK3, TRUE3, FALSE3))
The above give 4 potential results based on only 3 checks. Another option would be to do a check and supply a value for a TRUE result and another IF for a false result. Keep repeating the process. Conversely you could go the same route flipping TRUE FALSE option. It might look something like this:
IF(CHECK1, TRUE1, IF(CHECK2, TRUE2, IF(CHECK3, TRUE3, FALSE3)))
FALSE3 would be the result of all previous checks failing.
So for your case, your nested IF could look like (assuming the only valid entries are 1, 2, 3 and 4):
IF(A1=1,11,IF(A1,2,22,IF(A1=3,33,44)))
OR
IF(ISODD(A1),IF(A1=1,11,33),IF(A1=2,22,44))
and there are other options to work through the logic. there are also other checks you could be doing and results being displayed if your entries in A1 were not limited to the integers 1,2,3 and 4.
Now because you example is using the sequential integers 1,2,3 and 4 you could also use the CHOOSE function. Alternatively if you can make your criteria evaluate to sequential integers stating at 1 the CHOOSE function would work as well. Supply choose with an integer as the first argument and it will return the corresponding argument in the list that follows
CHOOSE(ARGUMENT,RESULT1, RESULT2,...,RESULTn-1, RESULTn)
In your case it would look something like:
CHOOSE(A1,11,22,33,44)
If you can not get sequential numbers for whatever reason and the gap is numbers is small and you are in the low integer count, you could leave a gap in results by providing "", or 0). lets say you has 1,3 and 4 as potential arguments, then your choose might look like:
CHOOSE(A1,11,"",33,44)
=IF(A1<>"",INDEX({11;22;33;44},A1),"")
=IF(AND(ISNUMBER(A1),A1<=4),A1*11,"")

Having trouble in nested IFs in excel with true or false values in each IF statement

=IF(AND(A2<=20151231),(B2=0) 0, 15, IF(AND(A2>=20190101,B2>=2),15, 7.5))
This is what I entered in the function.
if A2 is less than 20151231 and B2 is equal to 0 the value will be 0.
if A2 is greater than 20190101 and B2 is equal to or greater than 2 the value will be 15.
the problem is that excel says that I entered too many arguments and when I try to derive it it says that there is something wrong with the function I entered.
Try this:
=IF(AND(A2<=20151231,B2=0),"0",IF(AND(A2>=20191010,B2>=2),15,""))
it seems your formula has too much close and open parenthesis. When using "and()" enclose all logic in one set of parenthesis.
Hope this helps. Thanks
Reymond's answer is correct, assuming that you want the result to be:
0 - if A2<=20151231 AND B2=0
15 - if A2>=20190101 AND B2>=2
7.5 - if neither of these cases are true.
If you are struggling to see what parameters you are passing to which function, I would suggest that you format your formulas so that they are easier to read:
=IF(
AND(A2<=20151231, B2=0),
0,
IF(
AND(A2>=20190101,B2>=2),
15,
7.5
)
)
This makes it much easier to see what is going on and it can even be done in the excel formula bar if you wish (by using Alt+Enter):

Excel comparison with multiple IFs

I have a scale, based on which I decide the value of the coefficient for the multiplication. The scale looks as following:
Which means that:
for Category1: when value>=1.000.000 then coef is 1, when value>=500.000 then coef is 0.8 and etc.
Same logic applies for Category2;
Then I have input data in the following format:
Company !MainCat|Sales Amount|
Company1|T1 | 6.500.000|
Company2|T2 | 70.000|
I need to find corresponding coefficient, ratio of the coeffitient and the value (=ratio*MaxCoef). Currently, I am finding coef the following way:
- for company1:
=IF(C8>=$D2;$D$1;IF(C8>=$E2;$E$1;IF(C8>=$F2;$F$1;IF(C8>=$G2;$G$1;IF(C8>=$H2;$H$1;IF(C8>=$I2;$I$1))))))
That is literally hardcoded and doesn't look good. Maybe there is a better way of doing ? Any suggestions?
Formula view:
You can COUNTIF(range, [criteria] < value) * 0.2 as your add 0.2 per coef stage.
To you data do: =COUNTIF(D2:H2, "<"&C8) * 0.2, count how many stages the value passes * the value per stage.
Your count if range needs to be until H2 as I2 is 0, so inferior to value and gets counted.
To combine the COUNTIF() with a dynamic search for the right category based on MainCat you can MATCH() the MainCat with Code which will give the row where the Code is located and utilize INDIRECT() to apply it as range.
=COUNTIF(INDIRECT("D"&MATCH(B8,B:B,0)&":H"&MATCH(B8,B:B,0)),"<"&C8)*0.2
MATCH(B8,B:B,0) - will match the value on B8 (lets say T1) and return the row 2.
INDIRECT("D"&MATCH(B8,B:B,0)&":H"&MATCH(B8,B:B,0) = INDIRECT("D"&2&":H"&2) - will turn the text into an actual range to be use by the COUNTIF().
Create a table ‚Mapping’ that contains two columns, ‚Category’ and ‚Coefficient‘, then use INDEX-MATCH on it as described in https://www.deskbright.com/excel/using-index-match/.
=INDEX(Mapping[Category]; MATCH([Coefficient]; Mapping[Coefficient]; -1))
This example assumes that you put this formula into a table that has a column named ‚Coefficient‘ with the input value to your multiple IFs.
The trick is that as a match_type argument, provide either -1 or 1, according to your needs.
You can do this in VBA. Write your own function which ends in something like that
=MyOwnScale(C8; B8; A2:I3)
The first parameter of your VBA-function is the value, the second the category and the third is the range with the thresholds. So you can move your cascading IF-loops in VBA-Code and you (and your users) see only a clean function call in the cell.

If statement depending on time of day

I need to use a specific time, so far I had this
=IF(AND(TIME(15,45,0)<=(AW15=$A$11,$A$13)),IF(AND(TIME(15,45,0)>=(AW16=$A$13,$A$15))
So if before 15:45:00 I need for it to equal this logical test AW15=$A$11,$A$13 and if after 15:45:00 to equal to AW16=$A$13,$A$15.
Hope it makes sense and thanks in advance.
So it sounds like you are trying to write a nested IF statement. That is, IF A is true, THEN IF B is also true THEN return result 1, otherwise (A true, but B not true) return result 2, otherwise (A not true, B not tested) return result 3.
In Excel, this would be written as follows:
=IF(Parameter1=Condition1,IF(Parameter2=Condition2,Result1,Result2),Result3)
Applying it to your scenario, I think you are aiming for this:
=IF(TIME(HOUR(NOW()), MINUTE(NOW()), SECOND(NOW())) < TIME(15,45,0),
IF(AW15=$A$11,$A$13,"Condition1.2"),
IF(AW16=$A$13,$A$15,"Condition2.2"))
Note, some scenarios have not been covered by your statement, so I have written "Condition1.2" and "Condition2.2" which you can replace with additional tests or results to return.
Condition1.2 is where the time is before 15:45, but AW15 did NOT equal A11.
Condition2.2 is where the time is at or after 15:45, but AW16 did NOT equal A13.
You don't have to put anything in those placeholders if you don't want to, but if either of those conditions are ever met then the formula will simply return "FALSE".
Also, if you do not want the test time to be NOW(), then you will need to reference another cell that contains a fixed timestamp for when the row is being worked. NOW() is volatile, which means if you save the spreadsheet before 15:45 but then open it again after 15:45, the results you had already calculated will all change.
If i understand your question correctly, you need to compare the time now to 15:45:00 and make a selection based on that. If so the solution is:
=IF(TIME(HOUR(NOW()), MINUTE(NOW()), SECOND(NOW())) < TIME(15,45,0),
AW15=$A$11,$A$13, AW16=$A$13,$A$15)
This translates to: If the time now is before 15:45:00 then do AW16=$A$13,$A$15 else do AW16=$A$13,$A$15
If you want to compare the time in a specific cell then substitute the TIME(HOUR(NOW()), MINUTE(NOW()), SECOND(NOW())) with a cell which has a time for example:
=IF(A1 < TIME(15,45,0),
AW15=$A$11,$A$13, AW16=$A$13,$A$15)

Resources