In a spreadsheet I'm using, some of the possibilities for a cell is being blank, having the number zero, or having a non-zero number, or having a string.
If I want to test if the cell is zero, but is not blank, I currently do
=IF(AND(B2=0,LEN(B2)>0),foo,bar)
Is there a way of replacing the two conditionals with one conditional?
You can certainly use the function isblank(B2) instead of testing for length > 0.
But since you're testing for two conditions: not blank, and 0, I don't see how you can avoid two conditionals--unless--you're happy that 0 equates to not blank (which it does), then all you have to test for is 0.
The function you are looking for is the ISBLANK() function.
=IF(AND(ISBLANK(A1)=FALSE,A1=0),"foo","bar")
EDIT:
Sorry I just re-read your question. You're forced to use 2 conditionals because you're checking for two conditions. If you wanted to just use one condition for whatever reason the only way that I can think of is to extract the conditions you are checking into a method and then call it.
So, it becomes
=IF(CheckCellIsOK(A1),"foo","bar")
The method still checks both conditions but you're just hiding that fact.
Related
I would like to set a multiple IF statement for my formula.
The reason behind it is, that I need to switch off the values (mark them as N/A and NA) which are equal to the other certain values as follows:
LP372/24/60+1=($NO372-720)/24/60+1 => mark them as N/A - the values, which don't happen during the midnight
and
LP372/24/60+1=$NO372/24/60+1 => mark them as NA - the values, that don't happen during the midday
The problem is, that I can't keep them under one IF statement, because I need the division.
So far I used only one IF statement (the nested one), which looks as follows:
=IF(OR((LP372/24/60+1)=(($NO372-720)/24/60+1), OR((LP372/24/60+1)=($NO372/24/60+1))),"N/A",LP372/24/60+1).
The value of NO372 corresponds to the rough time of the local midday. Substracting the 720 we are getting the rough midday for this location, knowing that the single day counts 24 hours x 60mins = 1440mins.
The formula above gives a nice result, but without the division between midday and midnight, which I am talking about.
Following my older query about the similar issue:
Double nested IF statement formula in Excel
I tried to solve it like this:
=IF(COUNTA(E1116:E1481),IF(OR((LP372/24/60+1)=(($NO372-720)/24/60+1),OR((LP372/24/60+1)=($NO372/24/60+1))),"N/A",LP372/24/60+1),"NA")
where the range E1116:E1481 corresponds to the whole year period (1.01 - 31.12).
Unfortunately I got nothing, similarity to this formula:
=IF(COUNTA(E1116:BC1116),IF(OR((LP372/24/60+1)=(($NO372-720)/24/60+1),OR((LP372/24/60+1)=($NO372/24/60+1))),"N/A",LQ510/24/60+1),"NA"),
where the E:1116:BC1116 range correspond to the all columns - circumstances as occur on January 1.
This formula was still not correct.
I found something on the web about the multiple IF statements:
which led me to the following formula
=IF((LP372/24/60+1)=(($NO372-720)/24/60+1);"N/A";IF((LP372/24/60+1)=($NO372/24/60+1);"NA")LP372/24/60+1)
Unfortunately again not correct.
The last attempt was an answer provided here:
Multiple IF statements in Excel
on which basis I built up another formula
=IF((LP372/24/60+1)=(($NO372-720)/24/60+1),"N/A",LP372/24/60+1,IF((LP372/24/60+1)=($NO372/24/60+1),"NA"))
this time Excel said, that I've entered too many arguments.
Is there a way to set up multiple IF statements?
I need to:
make the values equal to midnight as N/A,
make the values equal to midday as NA
keep the other values as they stand
This question was answered in the comments, but I thought I would add an answer with an example of how to nest IF statements for anyone that found this question because they are having issues with nesting IF statements.
When you add an IF statement to a formula in excel, you have to keep in mind that IF statements have 3 parts separated with commas:
The first value is the condition you are going to check
The second value is the value you want returned if that condition is true
The third value is the value you want returned if that condition is false
So here's an example that illustrates that:
=IF(condition to check, value if condition is true, value if condition is false)
Here is an example IF statement:
=IF(A1="Hello","World","something else")
If you are getting FALSE returned then your IF statement probably looks like this:
=IF(A1="Hello","World")
Notice that there are only 2 values inside this IF statement: the condition you are checking (A1="Hello") and what you want displayed if that is true ("World"). FALSE would be displayed if the value in A1 is something other than Hello because you haven't included the third value in the IF that defines what you want displayed if A1 is not Hello (this value is listed as something else in the example above)
It's best to try and avoid nesting IF statements if you can because they can be hard to keep track of. I would suggest using AND() or OR() instead if you can. They are very similar to each other, you list multiple conditions within the parens separated by commas that you will be checking against, and both evaluate to either True or False. The only difference is that for AND() every condition within the parens needs to be true for it to evaluate to True where OR() only one of the conditions listed in the parens needs to be true for it to return True.
So, for example, if you wanted to display World if A1 is Hello or Hi and you were to use nested IF statements it would look like this:
=IF(A1="Hello","World",IF(A1="Hi","World","something else"))
which in my opinion is really ugly. however, if you used OR() instead of nesting IF statements it would look like this:
=IF(OR(A1="Hello",A1="Hi"),"World","something else")
AND() works the same way, but instead of just needing one condition within the parens to be true to return True as OR() does, with AND() all conditions inside of the AND() need to be true to display your value instead of just one. here's an example AND():
=IF(AND(A1="Hello",B1=""),"World","something else")
in this case "World" would be displayed only if A1 is Hello AND B1 is empty
for both AND() and OR() you can have more than just 2 values inside if you need to check for more than 2 conditions, you just need to separate them each with commas, like this:
AND(condition1, condition2, condition3, ....)
OR(condition1, condition2, condition3, .....)
but, if you have to use nested IF statements, the easiest way I've found is to type IF( , , ) first thing to make sure you are allowing for all 3 values that need to be inside of the IF and you have the right amount of values inside the IF. If you are getting the message You've entered too many arguments for this function it is because you have entered more than 3 values inside your IF, like this:
=IF(A1="Hello","World","something else","this value is causing the issue")
so, to properly nest an IF in your formula, type IF( , , ) first off where you want the IF statement to be in your formula and then just fill in the blanks, then you avoid entering too few values which results in FALSE being displayed if your condition is not true or You've entered too many arguments for this function because you entered more than 3 values in your IF
Hope this helps to understand IF statements and how to nest them properly.
Okay so my question is how do i use IF with 3 arguments while one of the arguments is "Ignore blanks".
If A1 is "Escalation_complaint or "Escalation_request" B1 must show "Escalated".
If A1 is any other text, B1 must show "Solved"
If A1 is blank, B1 must remain empty.
Can someone please help me figure this out?
Your first test must be whether the A1 is empty or not. This must be the first because if you test whether an empty string is contained in another string the answer is always yes. =SEARCH("","Something") returns 1. So, How do you test for an empty cell? I recommend Excel's COUNTA() function but you may prefer testing for "". So, let's say, your first test is this.
=IF(COUNTA(A1), True, False)
Some would say IF(COUNTA(A1)>0, True, False) and that is equivalent. The simple IF(COUNTA(A1) tests for non-zero. Any number other than zero returns True.
So, now you got your basic function. What's supposed to happen if the result is True? What do you want to happen if the result is False? The latter is easy. If the result is False you want a null string returns. So, now your formula looks like this:-
=IF(COUNTA(A1), True, "")
Observe that we simply replaced the False with the desired output. So, what do you want to happen if A1 has something in it, if the first test resulted in True? There are two possibilities. That makes the problem solvable with a single IF. IF A1 has something starting with "Escalation" in it the result should be "escalate", else it should be "solved". So, how do you test for cell content?
=Find("escalation", A1) will not find "Escalation" (with a capital E). Therefore I recommend SEARCH(), which does.
=SEARCH("escalation", A1) should return 1. If it returns 1 your problem is solved because if it returns any other number its not "escalated" but "solved". Unfortunately, there is a third option. It might return an error. In fact it will return an error every time the word isn't found. That gives you a 3-way possibility (1, bigger than 1, or Error) which can't be solved with a single IF. So, I suggest to avoid the error.
=Find("escalation", A1 & "escalation") will find the word "escalation" every time. But it will find it in first position only if the conditions for "Escalate" is met. Therefore the formula for the True condition in the basic formula must be this:-
IF(SEARCH("escalation", A1 & "escalation") = 1, "Escalated", "Solved")
That's it. Assemble the second IF into the first and you're done.
So, using two if(), and minimising the testing you need :
=IF(left(A1,3)="Esc","Escalated",IF(A1="","","Solved"))
Edit: Based on a comment to another answer, if testing for "esc" only could lead to problems then how about:
=IF(left(A1,5)="Escal","Escalated",IF(A1="","","Solved"))
Which will avoid words like "escaped" or "escargot"...
If you know that the text will always be Escalation_complaint or Escalation_request, you can do this with a nested IF query. =IF in Excel takes three parameters: what to verify, result if true, and result if false. You can have another IF statement in the what to do if it returns false section. We can also do an OR() statement to verify both Escalation_complaint and Escalation_request. The result looks like this:
=IF(OR(A1="Escalation_complaint",A1="Escalation_request"),"Escalated",IF(A1="","","Solved"))
The order here is important because you want the "for all other cases" to be in the final IF statement.
If you have a situation where you could also have "Request_escalation" or "complaint_escalation", you should make a more general solution that uses SEARCH or FIND which would allow you to do a more general lookup that allows for more values without having to hardcode them. Here is an example that makes it possible to find the word Escalation anywhere in the cell:
=IF(IFERROR(SEARCH("escalation",A1,1),0)>0, "Escalated", IF(A1="","","Solved"))
Excel have maximum 30 arguments in a formula.
= if (A1=1, "True",if(B1>2,"True", "false"))
Understand this pattern. easy way.
If you no longer understand this, or if your problem has not been resolved, attach a file or screenshot that relates to your problem.
I have column I as a calulation column and this is what I currently wrote.
and this gives me nothing.
=IF(B2<>""&D2<>"",B2*D2,IF(B2<>""&D2=""&C2<>"",B2*C2,IF(A2<>""&C2<>""&AND(B2&D2=""),A2*C2,IF(A2<>""&C2=""&D2<>""&B2="",A2*D2,A2*C2))))
The logic is if B2 and D2 are not null multiply b2*d2
if B2 is not null and D2 null then b2*c2
If B2 is null and D2 is not null then a2*d2
else a2*c2
Is any ways to make this code work?
Thank you
Alternative ways or rewriting your formula:
=IF(AND(B2<>"",D2<>""),B2*D2,IF(D2="",IF(B2<>"",B2*C2,A2*C2),IF(D2<>"",A2*D2,A2*C2)))
=IF(AND(B2<>"",D2<>""),B2*D2,IF(AND(B2="",D2=""),A2*C2,IF(D2="",B2*C2,A2*D2)))
They will make negligible difference in performance and what not. BruceWayne's answer is probably more readable in terms of following your logic and therefore easier to maintain or understand in the future. The above answers should provide the same results but are a few characters shorter in length.
And as a wacky alternative for thinking potentially outside the box:
=CHOOSE(SUM((B2<>"")*2+(D2<>""))+1,A2*C2,A2*D2,B2*C2,B2*D2)
Expanding (not just my waist size)
I had time on my hands so I was fooling around with the concept of TRUE and FALSE being equal to 1 and 0 when sent through a math operation. When I started looking at the options this reminded me of how a binary number works. Not that I have bgiven it too much thought, but I think it works because the options for each cell are binary or TRUE/FALSE. Since every possible combination was covered with a unique out come, I just had to come up with a formula that would produce unique results. In this case I just took the converting a a binary number approach. The key is TRUE = 1 after a math operation and FALSE = 0. Now going the other direction is not quite the same but as Jeeped once put it, 0 is FALSE and everything else is TRUE. so 3, -3, and 3.14 are all treated as TRUE if using the numerical values as the outcome of a logic check.
=IF(3.14,"THIS NUMBER IS TRUE","ONLY 0 IS FALSE")
So less side tracking and back on point (not sure how much I need to expand to!).
Looking at the table above, you will note in the yellow area, all possible combination for BLANK and NOT BLANK are listed. If you then assign a value to the column the same way binary numbers are (note row A) you can then start generating all the possible numbers
I started by generating the list I needed in E2:E5 for numbers that CHOOSE could work with. I assumed 0 would beat up CHOOSE and cause it to fail. I knew that FALSE+FALSE=0 and I also knew that TRUE+TRUE=2 and that both TRUE+FALSE=1 and FALSE+TRUE=1. I needed a way to distinguish the later two and I knew I needed a total of 4 results. That is when binary counting/conversion whatever you want to call it kicked in. I placed the following formula in D2 and copied down
=SUM((A2<>"")*2+(B2<>""))
Note the brackets around the logic check and
that the logic checks are sent through a math
operation before being summed.
technically speacking it is really:
=SUM((A2<>"")*2+(B2<>"")*1)
however the *1 is not needed
once I had that list generate, it was a simple +1 added to it to get into the 1 to 4 range seen in E2:E5.
Now that I had a way of generating the index number the only thing left to do was to match up the required results/formula with the right combination.
=CHOOSE(SUM((A2<>"")*2+(B2<>""))+1,"A","B","C","D")
Well I felt like I was beating a dead horse there for a bit, so if I over explained, my apologies. If there is something still missing ask for more explination.
UPDATE TID BIT
IF there were more columns to check it may be possible to adjust the choose formula by simply adding the next binary value to the column and making sure there is an appropriate number of results in the choose list. There should be 2^(# of columns to check) options
=CHOOSE(SUM((A2<>"")*4+(A2<>"")*2+(B2<>""))+1,"A","B","C","D","E","F","G","H")
Which kind of beats multiple nested IFs for brevity, but I think I finds the nested IFs easier to understand.
You should be using AND():
=IF(AND(B2<>"",D2<>""),B2*D2,IF(AND(B2<>"",D2=""),B2*C2,IF(AND(B2="",D2<>""),A2*D2,A2*C2)))
You seem to be mixing operators from other programming languages:
In Excel:
AND : binary operator : AND(TRUE, FALSE) => FALSE
& : concatenation : "Hello " & "World" => "Hello World"
I wrote an IF formula as follows...
=IF(AB2>=500000,"Platinum",IF(AB2>100000,"Gold",IF(AB2>0,"Silver","")))
This works perfectly fine, however I've been given a new caveat and I haven't been able to figure it out.
If Column labeled Sponsor (see below image) has a value, then it should become a "Platinum" tier.
So pretty much, I'm seeing if it's possible add this additional condition to my existing formula. Any help would be greatly appreciated!
You can just add an OR() to the IF statement that you already have.
=IF(OR(AB2>=500000,AA2<>""),"Platinum",IF(AB2>100000,"Gold",IF(AB2>0,"Silver","")))
=If(LEN(AA2)<> 0,"Platinum",if(AB2>50000,"Platinum .... etc.
However, your logic is going to get a bit twisted with the many nested IFs. You might instead consider using a CHOOSE > MATCH structure, thus:
=IF(LEN(AA2)<> 0,"Platinum",CHOOSE(MATCH(AB2,{0,100000,500000},1),"Silver","Gold","Platinum"))
This formula first checks to see if there's a sponsor, and if there is sets it to "Platinum"; if there isn't it proceeds to the CHOOSE(MATCH.
The MATCH looks for the largest number in the array (i.e., {0, 100000, 500000}) that is less than or equal to the value in AB2, and returns an index number for where it finds the match. The CHOOSE then selects that entry from the list and returns it.
I'm new to Excel, and I'm struggling with a formula. Essentially, what I'm looking for is to filter a cell through a set of procedures using a formula (this part isn't strict).
For example
Let's say I have a cell, A1. I'm trying to perform different calculations on this based on whether it is between a certain range of values. The problem is, it can be within several ranges.
Pseudo-Code representation
If(A1 > 187.5) {
// Run some code here.
}
If(A1 > 150) {
// Run some code here.
}
NOTE : The above example is only to illustrate the logic of sequential if statements.
Note that I Do not want a nested If statement. I'm just trying to run the same value through various checks. How do I do this in an Excel formula?
The best I can come up with is something like the following.
=(A1>187.5)*<some expression>+(A1>150)*<some expression>+....
The result of this will be some single value. (The straightforward way to get multiple values is to have the individual terms in separate cell.
If you want the result to reflect one among several mutually exclusive outcomes, then you would want to go with:
=(A1>187.5)*<some expression>+(A1>150)*(A1<=187.5)*...etc.
There are many ways to achieve this. One way is to use nested conditions like this:
=IF(Something, do something, IF(something else, do something, do something))
This is good if you want to condensate the formula a bit but arguably leads to more cluttered formulas. According to the FAST-Standard organization, those cases of nested conditions should be replaced by the use of flags. The most simple case would be, for example, where you would be looking for a rebate percentage according to a sales amount. In multiple cells you would have IF conditions evaluating to true only if the value matches that specific range. Then, your formula can be as simple as a SUMPRODUCT of your flags with your rebates percentages.
This is one example, but it can be applied to other cases very well too.
if there is a relationship between the numbers your checking for its very likely you can use
=CHOOSE(FLOOR(A1/160,1)+1,"<160",">160")
Which would put your 150 in the first and leave your 185 in the second