cs105 fall 20091 cs105 lab 3 – excel: the if function announcements mp 1 will be released on...
TRANSCRIPT
CS105 Fall 2009 1
CS105 Lab 3 – Excel: The IF Function
Announcements
MP 1 will be released on Monday 9/14, due Monday 9/28 by 9pm
Midterm 1 on Tuesday 9/29 at 7pm
Last day to request a conflict for Midterm 1: Thursday 9/24 by 7pm
CS105 Fall 2009 2
Lab Objectives
Learn the IF function. Learn to use nested IF. Learn to use Lookup Tables Learn to reference other worksheets in
Excel. Learn to create pie chart
Go to the course website and download the Excel Worksheet for Lab 3.
CS105 Fall 2009 3
A “real world” Scenario…
This spreadsheet contains sales data for a tea manufacturing company which sells 3 different brands of tea.
As an executive, you want to find out how much of the revenue comes from each product line, i.e : complete range E3:G26
We’ll use the IF function to do this.
CS105 Fall 2009 4
The IF Function Flowchart
Condition
True
False
Result for True
Condition
Result for False
Condition
CS105 Fall 2009 5
How Does IF Work in Excel?
Recall from lecture how the formula =IF(condition, result1, result2) works.
Excel looks at the condition. If it is true, result1 is returned. If it is false, result2 is returned.
The condition must be something that is either true or false; that is, the condition must be BOOLEAN.
CS105 Fall 2009 6
Setting Up Our IF Function
Our product names are in column A.
If a cell in column A matches cell E2 (Lively Lemon Tea), we want toput its revenue value (column D) in the Sales column for Lively Lemon Tea(column E). Otherwise we want to put 0 in that column.
CS105 Fall 2009 7
Display 0Product is
Lively Lemon Tea
True
False
Display revenue
Flowchart for Cell E3
How do we write the IF function?
=IF(A3=E2, D3, 0)
CS105 Fall 2009 8
Copying the formula
We must modify the relative references to absolute/mixed references before copying
Double-click on the fill handle to fill the column for “Lively Lemon Tea.”
Use the fill handle to copy the formula to cells F3 and G3.
Double-click on the fill handle in F3 and G3 to fill columns F and G.
=IF($A3=E$2, $D3, 0)
CS105 Fall 2009 9
Nested IF
You can have an IF function inside another IF function Handy if you have more than one condition
to test.
If “midterm is not this week” then “continue sleeping”, otherwise if “I’m not too sleepy” then “go to class”, otherwise “continue sleeping”
CS105 Fall 2009 10
The Nested IF Flowchart
Midterm is on tomorrow
Not too sleepy
Continue sleeping
Go to class
True
True
False
False
Continue sleeping
CS105 Fall 2009 11
The Nested IF Flowchart
Condition 1
Condition 2
Result for False Condition 1
Result for True Condition 1 andFalse Condition 2
Result for True Condition 1 andTrue Condition 2
True
True
False
False
This is not the only way nested IFs can be done. Can you think of some other ways?
CS105 Fall 2009 12
Calculating Eastern Region Sales
Suppose we want to compute the total sales for each product in the East region ONLY.
If column C contains the value that is “East”, then if the product name (in column A) matches corresponding tea product in H2:J2, display the revenue. Otherwise display 0
Fill in cells in collumn H,I,J.
CS105 Fall 2009 13
The Formula for Cell H3
=IF($C3="East",IF($A3=H$2,$D3,0) ,0)
Double-click on the fill handle to fill the column for “Lively Lemon Tea.”
Use the fill handle to copy the formula to cells I3 and J3.
Double-click on the fill handle to fill columns I and J.
CS105 Fall 2009 14
AND, OR, and NOT review
In lecture we saw AND, OR, NOT condition1 AND condition2
returns TRUE only when Both condition1 and condition2 must be TRUE
condition1 OR condition2 returns FALSE only when both condition1 and condition2 must be FALSE.
NOT(condition). Returns the logical opposite of condition.
CS105 Fall 2009 15
AND, OR, and NOT in Excel
In Excel, AND, OR, and NOT are functions.
AND(condition1, condition2). means condition1 AND condition2
OR(condition1, condition2). means condition1 OR condition2
NOT(condition). means the same as the normal meaning of NOT
CS105 Fall 2009 16
Using AND to Check Two Conditions
There is an alternative way to write the IF statement in cell H3. We want the revenue to be displayed in H3 if the product is “Lively Lemon Tea” AND the region is “East”:
=IF( AND( $C3="East", $A3=H$2), $D3, 0)
CS105 Fall 2009 17
Table Lookups
In this course, we use two Excel functions for doing table lookups: HLOOKUP and VLOOKUP
For HLOOKUP, we need to know The value that we’re looking up. The range of the table. Where its first row stores ascending
values. The row index that stores the looked up result
For VLOOKUP, it’s similar, except the table is vertical.
CS105 Fall 2009 18
Find Sale Rating
What formula to put in K3?
=HLOOKUP(D3,F28:J29,2) What are we missing? Fill in cells K4:K26
Where to put the “$”
The lookup value
Range addressof the table
The rating row index
CS105 Fall 2009 19
Working with Multiple Worksheets
Suppose we want to calculate the total tea sales…
Go to the ‘Statistics’ worksheet. In cell A8, put a formula to add up the range
E3:E26 of the 'Tea Data' worksheet. The formula is
=SUM('Tea Data'!E3:E26) Use the fill handle to copy the formula to B8
and C8. Compute the grand total of tea sales in cell D8.
Add a pie chart
Let’s add a pie chart in “Statistics” worksheet
Insert a Pie Chart You should know how
to Select data to display in
the chart Customize the “look-
and-feel” of the chart
CS105 Fall 2009 20
CS105 Fall 2009 21
What you should know?
The formula =IF($A3=E$2, $D3, 0) =IF($C3=“East”, IF( $A3 = H$2, $D3, 0), 0) =IF( AND( $C3="East", $A3=H$2), $D3, 0) =HLOOKUP(D3,$F$28:$J$29,2) =SUM('Tea Data'!E3:E26)
How to add and customize your pie chart
Let Excel do all the hard work for you !
Exercises
In columns K, L, and M, compute the sales of the teas for all regions except the west region.
In column N, find the sales of the teas for all regions during the months of January and February (Note: Do not split the sales up into multiple columns by Product).
Repeat question 2; but this time, do split the sales up into multiple columns by Product. Put these answers in columns O, P, and Q (Note: There are many possible solutions).
CS105 Fall 2009 22