You are on page 1of 33

Excel Logical Functions

Contents Guide to topics: For a result that...


Function Introduction
AND ...is a logical value (true or false) use
AND OR NOT AND, OR, NOT
IF
Nested IFs ...is another calculation or another value use
* Olive Oil Logic - 1 IF, AND, OR
* Olive Oil Logic - 2
* Olive Oil Logic - 3 ...filters and calculates use
IF Budget COUNTIF, SUMIF
IF AND
SUMIF ...is a calculation that multiplies and adds use
COUNTIF SUMPRODUCT
SUMPRODUCT

* For more on the olive oil logic exercises see the


"Proficiency Exercises & Solutions" document & workbook.
her value use

s and adds use


About functions Return to Contents

Functions are predefined formulas that perform calculations


by using specific values, called arguments, in a particular order,
or structure. Functions can be used to perform simple or complex
calculations.

Structure. The structure of a function begins with an equal sign (=),


followed by the function name, an opening parenthesis, the arguments
for the function separated by commas, and a closing parenthesis.

Function name. For a list of available functions, click a cell and press SHIFT+F3.

Arguments. Arguments can be numbers, text, logical values such as


TRUE or FALSE, arrays, error values such as #N/A, or cell references.
The argument you designate must produce a valid value for that argument.
Arguments can also be constants, formulas, or other functions.

Entering formulas When you create a formula that contains a function,


the Insert Function dialog box helps you enter worksheet functions.
As you enter a function into the formula, the Insert Function dialog box
displays the name of the function, each of its arguments, a description
of the function and each argument, the current result of the function,
and the current result of the entire formula.

In certain cases, you may need to use a function as one of the arguments
of another function. For example, the following formula uses a nested
AVERAGE function and compares the result with the value 50.

Valid returns When a nested function is used as an argument, it must return


the same type of value that the argument uses. For example, if the argument
returns a TRUE or FALSE value, then the nested function must return a TRUE
or FALSE. If it doesn't, Microsoft Excel displays a #VALUE! error value.

Nesting level limits A formula can contain up to seven levels of nested


functions. When Function B is used as an argument in Function A,
Function B is a second-level function. For instance, the AVERAGE function
and the SUM function are both second-level functions because they are
function would be a third-level function, and so on.
arguments of the IF function. A function nested within the AVERAGE

Note:
To switch between viewing the results of a function or formula and
viewing the formulas that return the results, choose one of the
following techniques:
1. Tools, Options, "View" tab, click "Formulas".
2. Press CTRL+` (grave accent).
3. Tools, Formula Auditing, choose "Formula Auditing mode".
Return to Contents
Excel's AND Function Return to Con

Formula Description (Result)


=AND(TRUE, TRUE) All arguments are true. Result: TRUE
=AND(TRUE, FALSE) One argument is false. Result: FALSE
=AND(2+2=4, 2+3=5) All arguments evaluate to true. Result: TRUE
=AND(2+2=4, 2+3=10) One argument is false. Result: FALSE

Notes on the AND Function


Returns TRUE if all its arguments are TRUE; returns FALSE if one or more argument is FALSE.
Syntax: AND(logical1,logical2, ...)
Logical1, logical2, ... are 1 to 30 conditions you want to test that can be either TRUE or FALSE.
Remarks
The arguments must evaluate to logical values such as TRUE or FALSE,
or the arguments must be arrays or references that contain logical values.
If an array or reference argument contains text or empty cells, those values are ignored.
If the specified range contains no logical values, AND returns the #VALUE! error value.
Return to Contents

TRUE or FALSE.
AND, OR, NOT

Data
15
9
8

Formula
=AND(B5>B6, B5<B7)
=OR(B5>B6, B5<B7)
=NOT(B5+B6=24)

Notes
All three of these functions test one or more conditions
entered as function parameters.
With AND, all conditions must be true to return TRUE.
With OR, at least one condition must be true to return TRUE.
With NOT, whatever is returned (TRUE or FALSE) is reversed.
Return to Contents

Description (Result)
Is 15 greater than 9 and less than 8? Result: FALSE
Is 15 greater than 9 or less than 8? Result: TRUE
Is 15 plus 9 not equal to 24? Result: FALSE

functions test one or more conditions


n parameters.
itions must be true to return TRUE.
one condition must be true to return TRUE.
er is returned (TRUE or FALSE) is reversed.
The IF Function

Data
50

Formula

=IF(B5<=100,"Within budget","Over budget")

=IF(B5=100,SUM(BigRange),"")

Notes on the IF Function


Returns one value if a condition you specify evaluates to TRUE
and another value if it evaluates to FALSE.
Use IF to conduct conditional tests on values and formulas.

Syntax: IF(logical_test,value_if_true,value_if_false)

Logical_test is any value or expression that can be evaluated to TRUE or FALSE.


For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100,
the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE.
This argument can use any comparison calculation operator.

Value_if_true is the value that is returned if logical_test is TRUE. For example,


if this argument is the text string "Within budget" and the logical_test argument
evaluates to TRUE, then the IF function displays the text "Within budget".
If logical_test is TRUE and value_if_true is blank, this argument returns 0 (zero).
To display the word TRUE, use the logical value TRUE for this argument.
Value_if_true can be another formula.

Value_if_false is the value that is returned if logical_test is FALSE. For example,


if this argument is the text string "Over budget" and the logical_test argument
evaluates to FALSE, then the IF function displays the text "Over budget".
If logical_test is FALSE and value_if_false is omitted, (that is, after
value_if_true, there is no comma), then the logical value FALSE is returned.
If logical_test is FALSE and value_if_false is blank (that is, after value_if_true,
there is a comma followed by the closing parenthesis), then the value 0 (zero)
is returned. Value_if_false can be another formula.

Remarks
Up to seven IF functions can be nested as value_if_true and value_if_false
arguments to construct more elaborate tests.
When the value_if_true and value_if_false arguments are evaluated,
IF returns the value returned by those statements.
If any of the arguments to IF are arrays, every element of the array
is evaluated when the IF statement is carried out.
Return to Contents

Description (Result)
If the value in Cell B5 is less than or equal to 100, then the
formula displays "Within budget". Otherwise, the function
displays "Over budget". Result: Over budget
If the number above is 100, then the range named
"BigRange" is summed. Otherwise, empty text ("") is returned.
Result: empty text

ify evaluates to TRUE

ues and formulas.

value_if_false)

that can be evaluated to TRUE or FALSE.


ion; if the value in cell A10 is equal to 100,
wise, the expression evaluates to FALSE.
alculation operator.

ned if logical_test is TRUE. For example,


budget" and the logical_test argument
displays the text "Within budget".
s blank, this argument returns 0 (zero).
value TRUE for this argument.

ned if logical_test is FALSE. For example,


budget" and the logical_test argument
displays the text "Over budget".
e is omitted, (that is, after
the logical value FALSE is returned.
is blank (that is, after value_if_true,
parenthesis), then the value 0 (zero)

alue_if_true and value_if_false

arguments are evaluated,

every element of the array


Nested IF Statements

Score
45
90
78

Formula
=IF(B5>89,"A",IF(B5>79,"B", IF(B5>69,"C",IF(B5>59,"D","F"))))
=IF(B6>89,"A",IF(B6>79,"B", IF(B6>69,"C",IF(B6>59,"D","F"))))
=IF(B7>89,"A",IF(B7>79,"B", IF(B7>69,"C",IF(B7>59,"D","F"))))

Notes:
In the examples above, the second IF statement is also the value_if_false
argument to the first IF statement. Similarly, the third IF statement is the
value_if_false argument to the second IF statement. For example, if the
first logical_test (Average>89) is TRUE, "A" is returned. If the first logical_test
is FALSE, the second IF statement is evaluated, and so on.

The letter grades are assigned to numbers using the following key.
If Score is
Greater than 89
From 80 to 89
From 70 to 79
From 60 to 69
Less than 60
Return to Contents

Description (Result)
Assigns a letter grade to the first score (F).
Assigns a letter grade to the second score (A).
Assigns a letter grade to the third score (C).

t logical_test

Then return
A
B
C
D
F
Olive Oil Logic - 1 Return to Contents

Cost/gallon for the first 500 gallons: $23


Cost/gallon for gallons above 500: $20
Logic Used for the Oliv
Number of gallons: Problem in the Proficie
10 Exercises

483
500
1600

Formulas to calculate the cost of:


10 gallons: $ 230 =IF(B7<=500,C4* B7, 500*C4+(B7
483 gallons: $ 11,109 =IF(B8<=500,C4* B8, 500*C4+(B8
500 gallons: $ 11,500 =IF(B9<=500,C4* B9, 500*C4+(B9
1600 gallons: $ 33,500 =IF(B10<=500,C4* B10, 500*C4+(
Logic Used for the Olive Oil
Problem in the Proficiency
Exercises

<=500,C4* B7, 500*C4+(B7-500)*C5)


<=500,C4* B8, 500*C4+(B8-500)*C5)
<=500,C4* B9, 500*C4+(B9-500)*C5)
0<=500,C4* B10, 500*C4+(B10-500)*C5)
Olive Oil Logic - 2 Return to Contents

Cost/gallon for the first 500 gallons: $23


Cost/gallon for the next 500 gallons: $20
Logic Used for the O
Cost/gallon for gallons >1,000: $15 Problem in the Profi
Number of gallons: Exercises

1,600
483
2001

Formulas to calculate the cost of: Check:


1600 gallons: $ 30,500 should be 30,500
483 gallons: $ 11,109 should be 11,109
2001 gallons: $ 36,515 should be 36,515
Logic Used for the Olive Oil
Problem in the Proficiency
Exercises

should be 30,500 =IF(B8<=500,B8*$C$4,IF(B8<=1000,(500*$C$4)+(B8-500)*$C$5,(500*$C$4)+(500*$C$5)+(B8-1000)*$C$6


should be 11,109 =IF(B9<=500,B9*$C$4,IF(B9<=1000,(500*$C$4)+(B9-500)*$C$5,(500*$C$4)+(500*$C$5)+(B9-1000)*$C$6
should be 36,515 =IF(B10<=500,B10*$C$4,IF(B10<=1000,(500*$C$4)+(B10-500)*$C$5,(500*$C$4)+(500*$C$5)+(B10-1000)
$4)+(500*$C$5)+(B8-1000)*$C$6))
$4)+(500*$C$5)+(B9-1000)*$C$6))
0*$C$4)+(500*$C$5)+(B10-1000)*$C$6))
Olive Oil Logic - 3 Return to Contents

More Logic Practice with a Different Layout.


The formulas here use MIN, MAX, a nested IF, and SUMPRODUCT.

Logic Used for the Olive Oil


Problem in the Proficiency 1600 483
Exercises gallons gallons
gals/price level gals/price level
First 500 gallons at $23.00 500 483
Next 500 gallons at $20.00 500 0
Any additional gallons at $15.00 600 0
Cost: $ 30,500 $ 11,109

=MIN(H$9, $D$12) =MIN(I$9, $D$12)

=MAX(IF(H$9<$D$12+ =MAX(IF(I$9<$D$12+
$D$13, H$9-$D$12, $D$13, I$9-$D$12,
$D$13), 0) $D$13), 0)

=IF(H$9>1000, H$9- =IF(I$9>1000, I$9-


1000, 0) 1000, 0)

=SUMPRODUCT($F$ =SUMPRODUCT($F$
12:$F$14, H12:H14) 12:$F$14, I12:I14)
2001
gallons
gals/price level
500
500
1001
$ 36,515

=MIN(J$9, $D$12)

=MAX(IF(J$9<$D$12+
$D$13, J$9-$D$12,
$D$13), 0)

=IF(J$9>1000, J$9-
1000, 0)

=SUMPRODUCT($F$1
2:$F$14, J12:J14)
IF - Budget example

Actual Expenses
$1,500
$500

Formula
=IF(B5>C5,"Over Budget","OK")
=IF(B6>C6,"Over Budget","OK")
Return to Contents

Budgeted Expenses
$900
$900

Description (Result)
Checks whether the first row is over budget. Result: Over Budget
Checks whether the second row is over budget. Result: OK
IF with nested AND

Data
50
104

Formula

=AND(1<B5, B5<100)

=IF(AND(1<B6, B6<100),B6, "The


value is out of range.")

=IF(AND(1<B5, B5<100), B5, "The


value is out of range.")

Notes:
In each formula above an AND function is
nested inside an IF function. The AND
function serves as the first parameter of the
IF function. If the AND function evaluates as
TRUE, then the 2nd IF function parameter is
returned. If the AND function evaluates as
FALSE, then the 3rd IF function parameter is
returned.
Return to Contents

Description (Result)

The value 50 is between 1 and 100. Result: TRUE

Displays the value 104 or displays a message. Result: The


value is out of range.

Displays the value 50 or displays a message. Result: 50

AND function is
n. The AND
parameter of the
ction evaluates as
ction parameter is
on evaluates as
ction parameter is
The SUMIF function Return to Contents

Property Value Commission


$100,000 $7,000
$200,000 $14,000
$300,000 $21,000
$400,000 $28,000

Formula Description (Result)


=SUMIF(B5:B8,">160000",C5:C8)
Sum commissions for property values over
$160,000. Result: $63,000.

Notes:
Syntax: SUMIF(range,criteria,sum_range)
Rangeis the range of cells you want evaluated.
Criteriais the criteria in the form of a number, expression, or text that defines which cells
will be added. For example, criteria can be expressed as 32, "32", ">32", "apples".
Sum_rangeare the actual cells to sum.
Remarks:
The cells in sum_range are summed only if their corresponding cells in range match the criteria.
If sum_range is omitted, the cells in range are summed.
$14,000
$21,000
$28,000
$63,000

fines which cells


, ">32", "apples".

match the criteria.


The COUNTIF function

Fruit Data
apples
oranges
peaches
apples

Formula

=COUNTIF(B5:B8,"apples")

=COUNTIF(C5:C8,">55")

Notes
Counts the number of cells within a range that meet the given criteria.

Syntax: COUNTIF(range,criteria)

Range is the range of cells from which you want to count cells.
Criteria is the criteria in the form of a number, expression, or
text that defines which cells will be counted. For example,
criteria can be expressed as 32, "32", ">32", "apples".
Return to Contents

Number Data
86
54
75
32

Description (Result)

Count the number of cells holding "apples" in the


"Fruit Data" column above. Result: 2

Count the number of cells with a value > 55 in the


"Number Data" column above. Result: 2

ithin a range that meet the given criteria.

om which you want to count cells.


orm of a number, expression, or
will be counted. For example,
32, "32", ">32", "apples".
Return to Contents
SUMPRODUCT

Array1 Array2
3 2
8 6
1 5
4 7
6 7
9 3

Formula Description (Result)

Multiplies the components of the


two arrays and sums the products
=SUMPRODUCT(B5:C7, D5:E7)
that is, 3*2 + 8*6 + 1*5 + 4*7 + 6*7 +
9*3. Result: 156

Notes:
Multiplies corresponding components in the given arrays, and returns the sum of those products.
Syntax: SUMPRODUCT(array1,array2,array3, ...)
Array1, array2, array3, ... are 2 to 30 arrays whose components you want to multiply and then add.

Remarks:
The array arguments must have the same dimensions. If they do not, SUMPRODUCT returns the
#VALUE! error value. SUMPRODUCT treats array entries that are not numeric as if they were zeros.
Return to Contents

urns the sum of those products.

you want to multiply and then add.

ot, SUMPRODUCT returns the


not numeric as if they were zeros.

You might also like