3 s2.0 B9780123746238500074 Main
3 s2.0 B9780123746238500074 Main
3 s2.0 B9780123746238500074 Main
Decision Functions
This chapter deals with making decisions or having a cell display
a value that is conditional upon what is in one or more other cells.
We begin with the logical comparison operators and Boolean
functions. We examine the IF function and show how to use
SUMIF, SUMIFS, and SUMPRODUCT. Then we examine the table
lookup functions LOOKUP, VLOOKUP andHLOOKUP. The chapter
concludes with some notes on conditional formatting.
Logical Comparison
Operators
Exercise 1: Boolean
Functions
Figure 5.1
By default, Excel aligns Boolean values (TRUE and FALSE)
centered horizontally in their cells.
Note that ifin this worksheet you entered the formula =A2>5,
the result would be TRUE. Excel would compare the letter a (a
text data type) with the literal 5 (also a text data type): the
ASCII value for a is 97, and that for 5 is 53.
There are some common combinations that are useful to know.
In the following table, Aand Bmay be expressions or references
to cells containing the values TRUE or FALSE. You may wish to
experiment with these nested formulas on Sheetl.
Logic
NAND
NOR
XOR
Exercise 2: Practical
Example
Formula
TRUE
=NOT(AND(A,B))
=NOT(OR(A,B))
=OR(AND(A, NOT(B)),AND(B, NOT(A)))
returned if
Not both true
Neither is true
Only one is true
Decision Functions 71
(a) On Sheet2 of Chap5.xlsx, enter the text and numbers shown
inAl:B16, C2:C3, and C5:E5 of Figure 5.2.
(b) Name C2:C3 as pmin and qtnax, respectively.
Figure 5.2
(c) The formula in C6 is =- -(A6>=pmin) and this is copied down
the column; if you remember the shortcut method of double
clicking the fill handle, you will need to delete C16.
(d) In D6 we need the formula =- -(B5<=qmax); and in E6
=- -AND(A6>pmin,B6>=qmax). These are to be copied down to
row 15. Can you think of a simpler formula in E6?
(e) Row 16 summarizes the results giving the percentage that
passed the tests. We might be tempted to use
=SUM(C6:C15)jCOUNT(C6:C15) in C16 to get the fraction
that passed the P-test. But this is just an average, so why not
use =AVERAGE(C6:C15) here and corresponding formulas in
D16 and E16?
(f)
The IF Function
(ii)
(iii)
(iv)
(v)
(vi)
=IF(A2<0, "Negative","Positive")
Returns the text "Negative" if A2 has a value less than 0;
otherwise it returns "Positive."
=IF(B5<>0, A5/B5, "") or =IF(B5<>0, A5/B5, NA())
Here is a way of preventing the #DIVO! error. These formulas
will return a blank or #Nj A when the divisor is zero,
otherwise the division result is returned.
An alternative formula is =IFERROR(A5/B5, "").
=IF(ABS(AlO- BlO)<=EPSILON, "Equal","Unequal")
A cell called EPSILON contains a value such as 1.0E-6. Rather
than doing a direct comparison of two values, we test if they
differ by more than this value.
=IF(ABS(AlO-BlO)<= 0.001,1,0)
Returns 1 if ABS(Al0- Bl0) is less than or equal to 0.001,
otherwise it returns O. We could also use the simpler formula
=- -(AlO-BlO<=O.OOl).
=IF(SUM(A12:A200, SUM(A12:A20), "Error")
If the sum of the range is greater than a, that value is
returned, otherwise the text "Error" is displayed.
=IF(Al, TRUE, FALSE) or =IF(Al<>O, TRUE, FALSE)
These will return the value TRUE if Al contains a nonzero
value, a formula giving a nonzero value, or the TRUE value. If
Al is empty or has the value a, the FALSE is returned. A
simpler formula would be =Al<>O, and this would be easier to
understand.
Decision Functions 73
(i)
Exercise 3: Resistors
Revisited
(e) Select B6:C6 and use the Merge and Center tool. Open the
Formatting dialog and give this cell the custom format
o"ohms." Test your worksheet.
(f)
Figure 5.3
(g) If this were a worksheet in the workplace, the user would
have no need to see row 4. Furthermore we would want to
ensure that the user could not inadvertently change any
formulas in that row. Right click the row header 4 and select
Hide. Later we will learn how to protect cell B6.
Exercise 4: Quadratic
Equation Solver
- b .J b 2 - 4ac
x=-----2a
The quantity b2 - 4ac is called the discriminant; its value
determines the number (0,1, or 2) of real roots of the equation.
Decision Functions 75
Figure 5.4
(a) Open Chap5.xlsx. On Sheet4 enter the values and text shown
in Al:C4. Select A3:C4 and horizontally center the entries
with the button in the Home / Alignment group.
(b) With A3:C4 still selected, use the command Formulas /
Defined Names / Create from Selection to give the cells A4:C4
the names in the cells above them.
(c) Type disc (short for discriminant) in E3. Enter the formula
=b*b - 4*a*c_ in E4. Center E3:E4 and create the name disc
for the cell E4.
(d) Temporarily ignore the entries in row 6.
(e) Type the text shown in A7 and C7. Enter these formulas
B7:
=(- b + SQRT(disc) )/(2*a)
D7: =(- b - SQRT(disc) )/(2*a)
(f)
(g) Enter the text in A6 and center this over A6:B6. In C6 enter
the formula =IF(disc<O, 0, IF(disc=O, 1, 2)). This returns 0
when the discriminant is negative, 1 when it is zero, and 2 in
all other cases.
(h) Replace the text in A7 with =IF(C6=0,"",IF(C6=1,"Double
Root","Root 1")). If there is one root, this returns the text
"Double Root," if there are two identical roots, it returns
"Root I." When there are no real roots, it returns an empty
test string.
(i)
(j)
Exercise 5: Protecting
a Worksheet
Do not use this type of
protection for supersensitive
information. Many websites
offer password breakers.
Decision Functions 77
Figure 5.6
Figure 5.5
Figure 5.7
Table Lookup
Functions
column_index_n um,
table_ array,
Decision Functions 79
The INDEX function returns an element from an array and has two
forms. The syntax of the first form is INDEX(array, row_num,
column_num). Thus =INDEX(Al:Cl0, 2, 3) returns the value atthe
intersection of row 2 and column 3 of the table Al:Cl0. In this
example, it returns the value from cell C2.
Exercise 6: A Simple
Lookup
Figure 5.8
(g) Get the same effect by moving E3:F6 down one row and
adding new data in E3. You will need to modify the formulas.
(h) Cut the table and paste it on a new sheet. Note how the
formulas in column Cautomatically adjust.
(i)
Since the table is (i) sorted and (ii) only two columns wide,
the LOOKUP function could be used in place of VLOOKUP.
Read Help and make the change.
Decision Functions 81
Figure 5.9
Exercise 8: Conditional The Excel functions SUMIF and COUNTIF may be used to
conditional sum or count a range of values. The values are
Summing
summed (or the entries are counted) subject to specified criteria
being satisfied. In the case of SUMIF,the range to be summed may
differ from the range to be tested.
Excel 2007 has the new functions SUMIFS and COUNTIFS where
multiple criteria may be specified. In addition, Excel 2007 has
=AVERAGEIF and AVERAGEIFS.
Unit
A
B
A
B
C
C
C
Problem
P2
P4
Pi
P4
P3
P6
P6
Downtime
9
14
20
26
14
12
14
Figure 5.10
Figure 5.11
Figure 5.12
To get a real appreciation of these functions, one needs a
moderately sized data set Rather than have the user make up
data, a file called ConditionalSums.xlsx is available on the
companion website. The first few rows of this are shown in Figure
5.10. Each column is named by the label in the top row. Scenario:
Amanufacturer has three identical productions units (A, B,and C)
and has kept a record of the problems they have incurred in a
certain time period and how much downtime there was to make
each repair/adjustment Figure 5.11 shows an example of various
formulas that could be used to analyze the data.
Decision Functions 83
Pivot tables are very useful for summarizing data; see Exercise 4
in Chapter 6. A sample pivot table made from the data in
ConditionalSumx.xlsx is shown in Figure 5.12.
Exercise 9: Array
Formulas
Figure 5.13
Figure 5.14
(a) On Sheet? of Chap5.xlsx, enter the text as in rows 1 and 3.
(b) SelectA4:A24, type =RANDBETWEEN(l,lOO) andcommitthe
formula with [Ctrll+[ .-J I (note there is no [0- ShiftI). This is an
alternative to typing the formula in one cell and then
dragging it down to row 24.
(j)
Decision Functions 85
Exercise 10:
Conditional
Formatting
This chapter has dealt with formulas that return values that
depend on a condition (tests, or criteria). In the last exercise we
look at conditional formatting: how to give cells a format that
depends on a criterion. We may wish values above (or below) a
certain value to be displayed in a different color or to be in cells
with an eye-catching background fill. Another use is to hide
certain values.
We will start by seeing how to hide specified formula values. In
Exercise 8 we had a cell that may return # Nj A. If we wished this
not to appear, we could change the formula in C5 from
=VLOOKUP(B5,$E$3:$F$6,2,TRUE) to the lengthy one: =IF
(ISERROR(VLOOKUP(B5,$E$3:$F$6,2,TRUE)),"",=VLOOKUP(B5,
$E$3:$F$6,2,TRUE)). But conditional formatting might be better.
(a) Select C5:Cl0 in SheetS. Use the command Home / Styles /
Conditional Formatting and in the resulting drop-down menu
select New Rules. In the next dialog box, click on Format cells
that contain. ... and select Errors (see Figure 5.15). Click the
Format button and on the Font tab set the color to match the
cell background color (most likely it will be white). Return to
the worksheet and the #NjA value is invisible.
(b) In the same worksheet, perhaps we would like a different
background color for the ppm depending on the value in the
corresponding C cell. Select B5:Bl0, open the Conditional
Formatting dialog, and select Use a formula to determine.....
Complete the resulting dialog as shown in Figure 5.16 and use
the Format button to select a background color. Repeat for
each possible value in Cusing different background colors.
Other conditional formatting examples are to be found in the file
ConditionalFormatxlsx on the companion website.
Figure 5.15
Figure 5.16
Exercise 11:
SUMPRODUCT
Figure 5.17
(a) Construct a worksheet similar to that in Figure 5.17.
(b) In cell B6 use =5UMPRODUCT(B3:F3,B4:F4)/5UM(B4:F4).
Decision Functions 87
II
Figure 5.18
We can see that there are three cases with Test 1 = a and Test 2 =
x. The corresponding numbers in row 5 are 2, 3, and 5, which sum
to 10. The formulas are as follows.
Column F
7
8
7
8
9
=SUMPRODUCT(--(B3:H3=lal),--(B4:H4=lx"))
=SUMPRODUCT(--(B3:H3=lal),--(B4:H4=l x"),BS:HS)
=SUMPRODUCT(--(B3:H3=la"),-(B4:H4=I X"),(BS: HS)A3)
Problems
1.
's.
Figure 5.19
2.
3.
4.
Decision Functions 89
5.
6.
Figure 5.20
7.
Figure 5.21
Again we need to protect against having more than one X in
a column. Use the same approach as in Chapter 4's problem
but with COUNTA rather than COUNT.
8.
Figure 5.22
(i)
(ii)
9.
Figure 5.23
10. In Bll Exercise 7 we ended up with
=VLOOKUP (B3*12,optimal,MATCH(B4,frame,O)+1)
(i) Modify it to return "l" if B3 is less than E2 or B4 has an
invalid value.
(ii) Make heights over E16 return "l]" and modifyB12:C12
to return nothing when Bll is "l" or "T!".