IF Function
Summary
The IF function can perform a
logical test and return one value for a TRUE result and another for a FALSE
result. For example, to "pass" scores more than or equal to 70:
=IF(C2>=70,"Pass","Fail")
More than one condition can
be tested by nesting IF functions. The IF function can be combined with logical
functions like AND and OR.
Purpose
Test for a specific condition
Return value
The values you supply for
TRUE or FALSE
Syntax
=IF (logical_test,
[value_if_true], [value_if_false])
Arguments
·
logical_test - A
value or logical expression that can be evaluated as TRUE or FALSE.
·
value_if_true -
[optional] The value to return when logical_test evaluates to TRUE.
·
value_if_false -
[optional] The value to return when logical_test evaluates to FALSE.
Usage notes
Use the IF function to test
for or evaluate certain conditions and then react differently depending on
whether the test was TRUE or FALSE.
Nested IF statements
The IF function can be "nested". A
"nested IF" refers to a formula where at least one IF function is
nested inside another in order to test for more conditions and return more possible
results. Each IF statement needs to be carefully "nested" inside
another so that the logic is correct. For example the following formula can be
used to assign a grade rather than a pass or fail result:
=IF(C6<70,"F",IF(C6<75,"D",IF(C6<85,"C",IF(C6<95,"B","A"))))
Note: Up to 64 IF functions can be nested.
Logical operators
When you are constructing a test with IF, you can use
any of the following logical operators:
Comparison operator |
Meaning |
Example |
= |
equal to |
A1=D1 |
> |
greater than |
A1>D1 |
>= |
greater than or equal to
|
A1>=D1 |
< |
less than |
A1<d1 |
<= |
less than or equal to |
A1<=D1 |
<> |
not equal to |
A1<>D1 |
IF with AND, OR
The IF function can be combined with the AND function
and the OR function. For example, to return "OK" when A1 is between 7
and 10, you can use a formula like this:
=IF(AND(B2>25,B2<50),"Pass","Fail")
=IF(OR(A2="Boy",A2="Girl"),”1”,
”0”)
Notes
·
To count things
conditionally, use the COUNTIF or the COUNTIFS functions.
·
To sum things
conditionally, use the SUMIF or the SUMIFS functions.
·
If any of the
arguments to IF are supplied as arrays, the IF function will evaluate every
element of the array.
If Monday, roll back to Friday
Generic formula
=IF(WEEKDAY(date)=2,date-3,date)
Explanation
To check the weekday of a date and roll back to Friday
when the date is a Monday, you can use the IF and WEEKDAY functions. In the
example shown, the formula in B1 is
=IF(WEEKDAY(A1)=2,A1-3,A1)
How this formula works
The WEEKDAY function returns a number 1-7 that corresponds
to particular days of the week. By default, WEEKDAY assumes a Sunday-based week
and assigns 1 to Sunday, 2 to Monday and so on with 7 assigned to Saturday.
In this case we only want to take action if the date
in question is Monday. To test, we use this expression inside the IF function:
WEEKDAY(A1)=2
If the logical expression returns TRUE, we know the date
is a Monday so we subtract 3 to "roll back" to Friday. If the
expression returns FALSE, we simply return the original date.
Comments
Post a Comment