If Condition in Excel
If Condition in Excel
In Excel, the IF function compares a value to an expected value and returns one of
two results, while the IFS function checks if one or more conditions are true and
returns the value that meets the first true condition:
IF function
Compares a value to an expected value and returns one of two results. The formula
is =IF(test, value-if-true, value-if-false). For example, =IF(C2=”Yes”,1,2) returns 1 if
C2 is "Yes" and 2 if it's not.
IFS function
Checks if one or more conditions are true and returns the value that meets the first
true condition. The formula is =IFS(logical_test1, value_if_true1, [logical_test2,
value_if_true2], [logical_test3; ...). The IFS function can handle up to 127
conditions.
The main difference between the IF and IFS functions is that IF only has one
conditional with two possible results, while IFS can handle a series of conditions.
Using IF with AND, OR, and NOT functions in Excel
Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel for the web Excel
2021 More...
In Excel, the IF function allows you to make a logical comparison between a value
and what you expect by testing for a condition and returning a result if that
condition is True or False.
=IF(Something is True, then do something, otherwise do something else)
But what if you need to test multiple conditions, where let’s say all conditions need
to be True or False (AND), or only one condition needs to be True or False (OR), or if
you want to check if a condition does NOT meet your criteria? All 3 functions can be
used on their own, but it’s much more common to see them paired with IF functions.
Technical Details
Here are overviews of how to structure AND, OR and NOT functions individually.
When you combine each one of them with an IF statement, they read like this:
AND – =IF(AND(Something is True, Something else is True), Value if True,
Value if False)
OR – =IF(OR(Something is True, Something else is True), Value if True, Value if
False)
NOT – =IF(NOT(Something is True), Value if True, Value if False)
Examples
Following are examples of some common nested IF(AND()), IF(OR()) and IF(NOT())
statements in Excel. The AND and OR functions can support up to 255 individual
conditions, but it’s not good practice to use more than a few because complex,
nested formulas can get very difficult to build, test and maintain. The NOT function
only takes one condition.
Here are the formulas spelled out according to their logic:
Formula Description
Note that all of the examples have a closing parenthesis after their respective
conditions are entered. The remaining True/False arguments are then left as part of
the outer IF statement. You can also substitute Text or Numeric values for the
TRUE/FALSE values to be returned in the examples.
Here are some examples of using AND, OR and NOT to evaluate dates.
=IF(NOT(A5>B2),TRUE,FALSE) IF A5 is not greater than B2, then return TRUE, otherwise ret
FALSE. In this case, A5 is greater than B2, so the formula retu
FALSE.
That's nice, but we can do even more powerful stuff with nested IF.
Nested IF Functions
Let's continue our previous example, but this time we want to have 3 age groups:
"child" when less than 18, "adult" when less than 80, and "old" otherwise. You
cannot do that with a single IF, 2 are needed:
=IF(AGE<18, "child", IF(AGE<80, "adult", "old"))
The first IF tests whether or not the person is a child, and if not, the second IF will
check if he is an adult or an old person.
This works fine, but if you start nesting more than 2 IF, thing will start to get hard to
read. That's why you might be interested in the IFS function.
IFS Function
IFS is a brand new function that does the same thing as nested IF, but in a clearer
way. It looks like this:
=IFS(test1, value-if-true, test2, value-if-true)
And you can put as many tests as you'd like. Note that this function only works in
Excel 2016 and above. If you try to use IFS in an older version, you'll see the
error #NAME?.
With IFS, our previous example can be re-written like this:
Before: =IF(AGE<18, "child", IF(AGE<80, "adult", "old"))
After: =IFS(AGE<18, "child", AGE<80, "adult", AGE>=80, "old")
Both lines do the exact same thing, but the second version is a little easier to read.
That's why I recommend using IFS instead of nested IF.
Logical Tests
Here's the list of all the logical tests you can do in an IF with examples:
Equal to: A1=A2.
Higher than: A1>A2.
Higher or equal than: A1>=A2.
Less than: A1<A2.
Less or equal than: A1<=A2.
Not equal to: A1<>A2.
This is pretty straightforward, except the "not equal to" that combines "higher than"
and "less than" at the same time.
Bonus
Here's a little game that I made, where players have to guess my favorite number.
How would you create that?
Here's the answer below. As you can see, you can make a simple game in excel in
just a line of code :-)
=IFS(C4<42, "too low", C4>42, "too high", C4=42, "you win!")
Conclusion----here's a quick summary of what we covered:
=IF(test, true, false)
=IF(test1, true, IF(test2, true, false))
=IFS(test1, true, test2, true, test3, true)