Anda di halaman 1dari 9

Student

Test1

Test 2

Test 3

Average

Paula

70

85

82

79

Cindy

85

92

90

89

Zach

100

91

96

96

Richie

80

72

66

73

Linda

74

65

81

73

Elisabeth

93

89

94

92

Nate

99

98

93

97

Gigi

100

78

92

90

Billy

61

67

56

61

Ginger

72

83

86

80

Ted

76

93

98

89

Jack

58

76

53

62

Results

=IF(E2>=85,"A",IF(E2>=75,"B",IF(E2>=63,"C","Fail")))
=IF(AVERAGE(B2,C2,D2)>85,"A",IF(AVERAGE(B2,C2,D2)>73,"B",IF(AVERAGE(B2,C2,
D2)>=63,"C","Fail")))

IF function
Use the IF function, one of the logical functions, to return one value if a condition is true and
another value if it's false.

Syntax
IF(logical_test, value_if_true, [value_if_false])
For example:

=IF(A2>B2,"Over Budget","OK")

=IF(A4=500,B4-A4,"")

Argument name

Description

logical_test
(required)

The condition you want to test. You can nest other logical functions within
this argument, including AND, OR and XOR functions.

value_if_true
(required)

The value that you want returned if the result of logical_test is TRUE.

value_if_false
(optional)

The value that you want returned if the result of logical_test is FALSE.

Examples
Copy the example data in the following table, and paste it in cell A1 of a new Excel worksheet.
To see the formula in a formula cell, select the cell and press F2.
Actual Expense

Predicted Expense

$1,500

$900

$500

$900

$500

$925

=IF(A2>B2,"Over
Budget","OK")

Because the actual expense of $1500 (A2) exceeded the predicted


expense of $900 (B2), the result is Over Budget .

=IF(A2<B2,TRUE,
IF(A3>B3,"over
budget","OK"))

The first IF function is false. Therefore, the second IF statement is


calculated and because it too is false, the result is OK.

=IF(A4=500,B4-A4,"")

Because A4 equals 500, the Actual Expense $500 is subtracted


from Predicted Expense $925 to tell you how much over budget
you are. The result is 425. If A4 didn't equal 500, then empty text
("") would be returned.

=IF(A2<B2,TRUE,
IF(A3>B3,"over
budget","OK"))

The first IF function is false. Therefore, the second IF statement is


calculated and because it too is false, the result is OK.

Second Example - nesting logical operators and working with text


In this example, you see uses of the AND and OR logical operator functions along with different
uses of nested IFs for tracking expense accounts broken out into flight costs and expenses. Copy
the example data in the following table, and paste it in cell A1 of a new Excel worksheet. To see
the formula in a formula cell, select the cell and press F2.

Sales Rep

Paul Wilder

Expecte
Actua
Actual Expecte
d
l
Expense d Flight
Expense
Flight
s
Cost
s
Cost

$1,500

$900

$400

$700

Angela Smith

$500

$900

$800

$1700

Bill Jackson

$500

$925

$600

$900

Lily Payton

$1300

$800

$1200

$900

Von Miller

$2200

$7300

$400

$3000

Jenny Wilson

$700

$1050

$700

$600

=IF(AND(C2=<B2,E2<=D2),"Approve","Audit")

Uses an AND condition in which


both conditions must be met for a
submitted expense report to be
automatically approved: actual
submitted expenses must be lower
than a predicted estimate AND
actual flight costs must be lower
than the expected flight cost. If
both conditions are met, the result
is Approve. If either or both
conditions are not met, the result is
Audit.

=IF(AND(C2<=B2,E2<=D2),"Approve", IF(C2
>=5000,"Flag for Excess","Audit"))

A more complex evaluation keeps


the AND conditions but uses a
nested IF in all cases that are not
marked "Approve." IF the spending
level is not approved, the nested IF
checks to see if a particular expense
is higher than $5000. IF the
expense is higher, the output is
Flag for Excess; if it is lower than
this arbitrary value, the output is
the milder Audit.

=IF(AND(C2<=B2,E19<=D19),"Approve",IF(C1>=5000,
(A2&" "&"has incurred excessive spending"),"Audit"))

The same evaluation is extended to


use concatenated text in the second
argument for the nested IF
function, adding some flexibility in
the text output. Pay close attention
to the use of parentheses and
double quote marks in the example
(A2&" "&, including the spacebar
character between the double quote
marks). The string "has incurred
excessive spending" is added to the
sales representative's name
(referred to by its cell number) for
the formula's output when this
condition is met.

The formula now uses an OR


function, indicating a more
forgiving approval process: if either
=IF(OR(C2<=B2,E2<=D2)"Approve",IF(C2>=5000,(A2&" actual expense category is equal to
"&"has incurred excessive spending"),"Audit"))
or lower than its expected expense
category, the travel spend is
approved. In practice, only a single
record is affected for this example.
The following diagram shows how a nested IF works within an argument:

When Excel finishes evaluating the first condition, the results may match (in which case the
Approve result appears) or they may not. If it's not a match, the parent IF function has already
run through two of its three arguments. You still have two possible outcomes! You complete your
formula by nesting your second IF function in the third argument (value_if_false) of the parent
IF. The nested IF becomes the self-contained third argument of the parent IF. When the nested IF
finishes evaluating, it decides between the two remaining possible outcomes, displays the result,
and the function ends.

Third Example - nested IFs in exam grading

In this example, you see the use of nested IFs in a popular application of exam grading averages.
Copy the example data in the following table, and paste it in cell A1 of a new Excel worksheet.
Place your formula in the far right Results column starting in Row 2, cell F2. To see the formula
in a formula cell, select the cell and press F2.
Student

Test1

Test 2 Test 3 Average

Paula

70

85

82

79

Cindy

85

92

90

89

Zach

100

91

96

96

Richie

80

72

66

73

Linda

74

65

81

73

Elisabeth

93

89

94

92

Nate

99

98

93

97

Gigi

100

78

92

90

Billy

61

67

56

61

Ginger

72

83

86

80

Ted

76

93

98

89

Jack

58

76

53

62

Results

Uses a sequence of three nested IFs to match


=IF(E2>=85,"A",IF(E2>=75,"B",IF(E2>=63," against the summed average of each student's three
C","Fail")))
exams, in Column E, for an entire course's lettergrade listing.

Applies an =AVERAGE math function within each


nested IF statement to compile the complete
average of all three exam scores and output the
=IF(AVERAGE(B2,C2,D2)>85,"A",IF(AVER
final result for each student. The formula grades on
AGE(B2,C2,D2)>73,"B",IF(AVERAGE(B2,C
a curve, with three passing grade levels of A, B
2,D2)>=63,"C","Fail")))
and C. If the first condition is met, the student
receives an A; if the second condition is met, a B,
and so on.

Fourth Example - Working with Dates


The IF function does not directly support date values for conditions in a formula. Because you
can nest other functions in an IF argument, you can use Excel day or date-related functions such
as TODAY and DATEVALUE to recognize dates in your IF function. The method is similar to
nesting logical operator functions (OR, AND) or math functions (SUM, AVERAGE). Copy the
example data in the following table, and paste it in cell A1 of a new Excel worksheet. Place your
formula in the far right Meeting Status column starting in Row 2, cell C2. To see the formula in
a formula cell, select the cell and press F2.
Team Meeting

Meeting Date

Planning 1

10-Jan

Planning 2

17-Jan

Design 1

24-Jan

Design 2

31-Jan

Design 3

6-Feb

Meeting Status

Design 4

13-Feb

Review 1

20-Feb

Review 2

27-Feb

=IF(B2<=DATEVALUE("2/6/2016"),
"Finished", "Upcoming")

The IF formula checks the dates in Column B and


outputs Finished if the meeting took place on or
before February 6th. If the condition isn't met, the
result is Upcoming.

Common Problems
Problem

What went wrong

In all Excel IF functions, the THEN is always implied. You do not explicitly
state a THEN argument in any IF formula; the logical_test argument, and
Where's the THEN
any nested IFs in value_if_true or value_if_False arguments imply the
function? Is there
"Then" actions. In the same way, Excel does not support an ELSE function.
an ELSE function?
ELSE is implied by the three arguments of the IF function (IF logical_test,
then value_if_true, else value_if_False.

Excel functions such as IF do not directly support the use of color as a


criteria for modifying the appearance of data, or performing operations such
I want to use cell
as firing off a calculation when a cell's color changes. Excel supports the use
colors for
of a set of Visual Basic for Applications (VBA) color functions in the
conditions in my IF
modColorFunctions module, for Excel operations on cells and tables
formulas.
employing color for data marking. You can nest these VBA functions within
IF function arguments. (Press Alt+F11 to open the VBA Editor.)

0 (zero) in cell

There was no argument for either value_if_true or value_if_False


arguments. To see the right value returned, add argument text to the two
arguments, or add TRUE or FALSE to the argument.

#NAME? in cell

This usually means that the formula is misspelled.

Problem

What went wrong

#VALUE! in cell

If none of the arguments in the function evaluate to a logical value, for the
logical_test, value_if_true or value_if_False arguments, you will receive a
#VALUE! error. This can occur if you make mistakes in defining your
arguments.

Best practices
Do this

Why

Carefully consider
when using IF
functions within
another IF.

Up to 64 IF functions can be nested within a parent IF function in the


logical_test , value_if_true and value_if_false arguments to construct
more elaborate tests. As a practical matter, the realistic limit for a usable
nested IF function is probably considerably less.

If any of the arguments to the IF function are arrays, every element of the
Use IF with arrays array is evaluated when the IF statement is carried out. Learn how to create
an array formula.

You can nest other logical expressions in a parent IF formula (other IF


functions, the AND function, the OR function, the XOR function, and
others) to perform more-sophisticated data tasks on a worksheet. Math
functions are another useful trick to nest within an IF argument. To do so
Nest other logic
effectively, you may consider writing out the logic of a complete calculation
functions to create
before writing a formula.
more-complex logic
formulas
IF formulas can get quite complex. The use of nested IF statements can lead
to slower performance and confusing logic. If you expect to use more than 5
nested IF functions in your formula, consider using functions such as
VLOOKUP function, HLOOKUP, and INDEX/MATCH.

Anda mungkin juga menyukai