+ All Categories
Home > Documents > 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS...

1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS...

Date post: 12-Jan-2016
Category:
Upload: regina-wilkins
View: 215 times
Download: 2 times
Share this document with a friend
19
1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Herbert G. Mayer, PSU CS Status 7/17/2013 Status 7/17/2013 Initial content copied verbatim from Initial content copied verbatim from CS 106 material developed by CS 106 material developed by CS professors: Cynthia Brown & Robert Martin CS professors: Cynthia Brown & Robert Martin
Transcript
Page 1: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

1

CS 106Computing Fundamentals II

Chapter 34“Conditionals In Excel”

Herbert G. Mayer, PSU CSHerbert G. Mayer, PSU CSStatus 7/17/2013Status 7/17/2013

Initial content copied verbatim fromInitial content copied verbatim fromCS 106 material developed byCS 106 material developed by

CS professors: Cynthia Brown & Robert MartinCS professors: Cynthia Brown & Robert Martin

Page 2: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

2

Syllabus Conditionals in FormulasConditionals in Formulas

Sample WorkbookSample Workbook

Basic Excel FunctionsBasic Excel Functions

Using SUMIFUsing SUMIF

Using COUNTIFUsing COUNTIF

Conditional FormattingConditional Formatting

Using FormulasUsing Formulas

Page 3: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

3

Conditionals in Formulas

• There are convenient ways to use conditionals in There are convenient ways to use conditionals in Excel formulas without actually programmingExcel formulas without actually programming

• For complex conditionals, a program is much easier For complex conditionals, a program is much easier to understandto understand

• For simple conditionals, though, the Excel functions For simple conditionals, though, the Excel functions offer convenient ways to create spreadsheetsoffer convenient ways to create spreadsheets

3

Page 4: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

4

Sample Workbook

• The Excel Workbook called Conditionals Example The Excel Workbook called Conditionals Example contains all the examples discussed in these slidescontains all the examples discussed in these slides

• It shows an imaginary Real Estate company’s It shows an imaginary Real Estate company’s spreadsheet, with sales prices for houses in various spreadsheet, with sales prices for houses in various neighborhoods, and which agent sold themneighborhoods, and which agent sold them

• It has a number of added conditional features which It has a number of added conditional features which we’ll discuss one by onewe’ll discuss one by one

Page 5: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

5

Basic Excel Functions

• The spreadsheet cells C18 and C19 show the average The spreadsheet cells C18 and C19 show the average price of properties in the sheet, done in two different price of properties in the sheet, done in two different ways – take a look at the formulas (by clicking on the ways – take a look at the formulas (by clicking on the cell and looking at the formula window)cell and looking at the formula window)

• But what if we want to know the average price for one But what if we want to know the average price for one salesperson?salesperson?

Page 6: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

6

Using SUMIF

• To find John M.’s total sales, we use the following To find John M.’s total sales, we use the following formula for cell C20:formula for cell C20:

=SUMIF(A7:A16, "John M.", C7:C16)=SUMIF(A7:A16, "John M.", C7:C16)

• This says to sum the elements of C7:C16 where the This says to sum the elements of C7:C16 where the corresponding element of A7:A16 equals “John M.”corresponding element of A7:A16 equals “John M.”

Page 7: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

7

Using COUNTIF

• To get the average sale for John M., we need to divide To get the average sale for John M., we need to divide by his number of sales. We can do this using by his number of sales. We can do this using COUNTIF in the formula for cell C21:COUNTIF in the formula for cell C21:

=C20/COUNTIF(A7:A16, "John M.")=C20/COUNTIF(A7:A16, "John M.")

• COUNTIF(A7:A16, “John M.”) returns the number of COUNTIF(A7:A16, “John M.”) returns the number of entries in the given range that equal John M.entries in the given range that equal John M.

• Note they must be Note they must be exactly the same!exactly the same!

• Blanks do count, hence can make a differenceBlanks do count, hence can make a difference

• Also, lower-case is different from upper-caseAlso, lower-case is different from upper-case

Page 8: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

8

Sales per Neighborhood

• We can use the same idea to get average and total We can use the same idea to get average and total sales per neighborhood. This is done in C22 and C23 sales per neighborhood. This is done in C22 and C23 for Laurelhurst using the following formulas:for Laurelhurst using the following formulas:

• For C22: For C22: = SUMIF(B7:B16,"Laurelhurst",C7:C16)= SUMIF(B7:B16,"Laurelhurst",C7:C16)

• For C23: For C23: = C22/COUNTIF(B7:B16,"Laurelhurst")= C22/COUNTIF(B7:B16,"Laurelhurst")

Page 9: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

9

A More Complex Condition

• Typical complex conditions may have the AND of two Typical complex conditions may have the AND of two or more conditionsor more conditions

• Excel has a built-in way to handle this using Excel has a built-in way to handle this using COUNTIFS and SUMIFSCOUNTIFS and SUMIFS

• Let’s look at how to figure out John M.’s total and Let’s look at how to figure out John M.’s total and average sales in Laurelhurstaverage sales in Laurelhurst

• That is, we want to count sales that are by John M. That is, we want to count sales that are by John M. provided it is in Laurelhurstprovided it is in Laurelhurst

Page 10: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

10

Using COUNTIFS and SUMIFS

John M.’s Laurelhurst Total (C24):John M.’s Laurelhurst Total (C24):

=SUMIFS(C7:C16, A7:A16, "John M.", B7:B16, "Laurelhurst")=SUMIFS(C7:C16, A7:A16, "John M.", B7:B16, "Laurelhurst")

Note the column to be summed comes first, followed by Note the column to be summed comes first, followed by the other columns and their conditionsthe other columns and their conditions

The equality test = is impliedThe equality test = is implied

And the And the Boolean ANDBoolean AND condition for both being true is condition for both being true is impliedimplied

John M.’s Laurelhurst Average (C25):John M.’s Laurelhurst Average (C25):

=C24/COUNTIFS(A7:A16,"John M.", B7:B16, "Laurelhurst")=C24/COUNTIFS(A7:A16,"John M.", B7:B16, "Laurelhurst")

Page 11: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

11

An OR Condition

• Suppose we want a condition that is an Suppose we want a condition that is an OROR instead of instead of an an ANDAND, or uses a comparison other than =, or uses a comparison other than =

• One way to do it is to make a column for the One way to do it is to make a column for the condition and then use the column values in a SUMIF condition and then use the column values in a SUMIF or COUNTIFor COUNTIF

• For example, let’s say we want the total sales for For example, let’s say we want the total sales for houses in houses in IrvingtonIrvington as well as in as well as in AlamedaAlameda

• Logically, this means the Neighborhood field is Logically, this means the Neighborhood field is Irvington Irvington OROR it is Alameda it is Alameda

Page 12: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

12

A New Column for the Condition

Column E on the spreadsheet is the result of testing Column E on the spreadsheet is the result of testing whether the neighborhood equals Irvington OR whether the neighborhood equals Irvington OR AlamedaAlameda

The formula looks like this:The formula looks like this:

=IF(OR(B7="Alameda",B7="Irvington"),"yes","no")=IF(OR(B7="Alameda",B7="Irvington"),"yes","no")

Note the format for the IF:Note the format for the IF:

IF(condition, true branch, false branch)IF(condition, true branch, false branch)

These IF’s can be nested. But you can see that too much These IF’s can be nested. But you can see that too much nesting would be hard to readnesting would be hard to read

Page 13: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

13

Using the Condition

C26 has the total sales for properties in Irvington and C26 has the total sales for properties in Irvington and AlamedaAlameda

Here is the formula, using Column E:Here is the formula, using Column E:

=SUMIF(E7:E16,"yes",C7:C16)=SUMIF(E7:E16,"yes",C7:C16)

Page 14: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

14

Conditional Formatting

• Along with conditionals in formulas, Excel also lets Along with conditionals in formulas, Excel also lets you specify formatting using conditionsyou specify formatting using conditions

• There is a conditional formatting button in the main There is a conditional formatting button in the main ribbon, shown on the next slideribbon, shown on the next slide

• (We did this on the windows version but it is very (We did this on the windows version but it is very similar on the Mac)similar on the Mac)

Page 15: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

15

Page 16: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

16

Data Bars

Column C shows an example of Data Bars formattingColumn C shows an example of Data Bars formatting

The colored bar in each cell is determined by the cell The colored bar in each cell is determined by the cell value, with the smallest cell having only a small value, with the smallest cell having only a small colored area and the largest one almost filledcolored area and the largest one almost filled

Page 17: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

17

Using Formulas

Column A shows an example of using a formula in Column A shows an example of using a formula in conditional formattingconditional formatting

The formula must have a Boolean valueThe formula must have a Boolean value

In this case I colored the cell if the sale was in In this case I colored the cell if the sale was in Laurelhurst and above the average price for Laurelhurst and above the average price for LaurelhurstLaurelhurst

Here is the formula:Here is the formula:

=AND(C7>$C$23, B7="Laurelhurst")=AND(C7>$C$23, B7="Laurelhurst")

Page 18: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

18

If you make a lot of formatting rules…

18

The Manage Rules item lets you edit and examine your rules

Page 19: 1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.

19

Try it Yourself!

Knowing how to use conditionals makes Excel even Knowing how to use conditionals makes Excel even more powerful more powerful

You should play around with the example spreadsheet You should play around with the example spreadsheet or one that you make yourself to understand the or one that you make yourself to understand the conceptsconcepts


Recommended