IF Statements-New
IF Statements-New
IF Statements
Assume that if you order up to 500 units of a product, you pay $3.00 per unit. If you order from 501
through 1200 units, you pay $2.70 per unit. If you order from 1201 through 2000 units, you pay $2.30
per unit. If you order more than 2000 units, you pay $2.00 per unit. How can you write a formula that
expresses the purchase cost as a function of the number of units purchased?
Your worksheet contains quarterly revenues for Wal-Mart. Can you easily compute the revenue for each
year and place it in the row containing the first quarter's sales for that year?
The situations listed above seem to have little in common. However, setting up Excel 2007 models for each of
these situations requires the use of an IF statement The IF formula is the single most useful formula in Excel. IF
formulas let you conduct conditional tests on values and formulas, mimicking (to a limited degree) the
conditional logic provided by computing languages such as C, C++, and Java.
An IF formula begins with a condition such as A1>10. If the condition is true, the formula returns the first value
listed in the formula; otherwise, we move on within the formula and repeat the process.
Assume that if you order up to 500 units of a product, you pay $3.00 per unit. If you order from 501 through
1200 units, you pay $2.70 per unit. If you order from 1201 through 2000 units, you pay $2.30 per unit. If you
order more than 2000 units, you pay $2.00 per unit. How can you write a formula that expresses the purchase
cost as a function of the number of units purchased?
You can find the solution to this question on the Quantity Discount worksheet in the file Ifstatement.xlsx. The
worksheet is shown in Figure 1. Figure 1. You can use an IF formula to model quantity discounts.
Suppose cell A9 contains our order quantity. We can compute an order's cost as a function of the order quantity
by implementing the following logic:
1
P. Batchelor Using IF Statements 2
We begin by linking the range names in A2:A4 to cells B2:B4, and linking the range names in cells D2:D5 to cells
C2:C5. Then we implement this logic in cell B9 with the following formula:
=IF(A9<=_cut1,price1*A9,IF(A9<=_cut2,price2*A9,IF(A9<=_cut3,price3*A9,price4*A9)))
To understand how Excel computes a value from this formula, recall that IF statements are evaluated from left
to right. If the order quantity is less than or equal to 500 (cut1), the cost is given by price1*A9. If the order
quantity is not less than or equal to 500, the formula checks to see whether the order quantity is less than or
equal to 1200. If this is the case, the order quantity is from 501 through 1200, and the formula computes a cost
of price2*A9. Next, we check whether the order quantity is less than or equal to 2000. If this is true, the order
quantity is from 1201 through 2000, and our formula computes a cost of price3*A9. Finally, if the order cost has
not yet been computed, our formula defaults to the value price4*A9. In each case, the IF formula returns the
correct order cost. Note that I typed in three more order quantities in cells A10:A12 and copied our cost formula
to B10:B12. For each order quantity, our formula returns the correct total cost.
Your worksheet contains quarterly revenues for Wal-Mart. Can you easily compute the revenue for each
year and place it in the row containing the first quarter's sales for that year?
The file Walmartrev.xlsx contains quarterly revenues (in millions of dollars) for Wal-Mart. (See Figure 3.) Rows
6, 10, 14, and so on contain the revenues for the first quarter of each year. In each of these rows, we would like
to compute total revenues for the year in column E. In other rows, column E should be blank. We could enter in
cell E6 the formula SUM(D6:D9) and copy this formula to E10, then E14, then E18, and so on. There must be a
better way. Using an IF statement with two neat Excel functions, (ROW() and MOD()), gives us an easy way to
enter our formula once and then copy the formula. The function ROW(cell reference) yields the row of
reference. The function =ROW(A6) would yield a 6, whereas if we are in row 6 the =ROW() function would also
yield a 6. The function MOD(number,divisor) yields the remainder when number is divided by divisor. For
example, MOD(9,4) yields 1, whereas MOD(6,3) yields 0. Note that we want our formula to work only in rows
that leave a remainder of 2 when divided by 4. Therefore, copying from E6 to E7:E57 the formula
=IF(MOD(ROW(),4)=2,SUM(D6:D9)," ") will ensure that we add up revenues for the current year only in rows
that leave a remainder of 2 when divided by 4. This means that we compute annual revenues only in the first
quarter of each year, as desired.
2
P. Batchelor Using IF Statements 3
Figure 2.
IF statements can get rather large. How many IF statements can be nested in a cell? What is the maximum
number of characters allowed in an Excel formula?
In Excel 2007, you can nest up to 64 IF statements in a cell. In previous versions of Excel, you could nest a
maximum of 7 IF statements. In Excel 2007, a cell can contain up to 32,000 characters. Previous versions of Excel
allowed only 1000 characters in a cell.
Problems
1.Suppose the price of a product will change at dates in the future, as follows:
Date Price
On or before February 15, 2009 $8
From February 16, 2009, through April 10, 2010 $9
From April 11, 2010, through January 15, 2011 $10
After January 15, 2011 $11
Using file IF-1.xlsx, write a formula that will compute the price of the product based on the date the
product is sold.
2.The Blue Yonder Airline flight from Seattle to New York has a capacity of 250 people. The airline sold 270
tickets for the flight at a price of $300 per ticket. Tickets are nonrefundable. The variable cost of flying a
passenger (mostly food costs and fuel costs) is $30 per passenger. If more than 250 people show up for
the flight, the flight is overbooked and Blue Yonder must pay overbooking compensation of $350 per
person to each overbooked passenger. Develop a worksheet that computes Blue Yonder's profit based on
3
P. Batchelor Using IF Statements 4