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 106 material developed by CS professors: Cynthia Brown & Robert Martin
2 Syllabus Conditionals in Formulas Conditionals in Formulas Sample Workbook Sample Workbook Basic Excel Functions Basic Excel Functions Using SUMIF Using SUMIF Using COUNTIF Using COUNTIF Conditional Formatting Conditional Formatting Using Formulas Using Formulas
3 Conditionals in Formulas There are convenient ways to use conditionals in Excel formulas without actually programmingThere are convenient ways to use conditionals in Excel formulas without actually programming For complex conditionals, a program is much easier to understandFor complex conditionals, a program is much easier to understand For simple conditionals, though, the Excel functions offer convenient ways to create spreadsheetsFor simple conditionals, though, the Excel functions offer convenient ways to create spreadsheets 3
4 Sample Workbook The Excel Workbook called Conditionals Example contains all the examples discussed in these slidesThe Excel Workbook called Conditionals Example contains all the examples discussed in these slides It shows an imaginary Real Estate company’s spreadsheet, with sales prices for houses in various neighborhoods, and which agent sold themIt shows an imaginary Real Estate company’s spreadsheet, with sales prices for houses in various neighborhoods, and which agent sold them It has a number of added conditional features which we’ll discuss one by oneIt has a number of added conditional features which we’ll discuss one by one
5 Basic Excel Functions The spreadsheet cells C18 and C19 show the average price of properties in the sheet, done in two different ways – take a look at the formulas (by clicking on the cell and looking at the formula window)The spreadsheet cells C18 and C19 show the average price of properties in the sheet, done in two different ways – take a look at the formulas (by clicking on the cell and looking at the formula window) But what if we want to know the average price for one salesperson?But what if we want to know the average price for one salesperson?
6 Using SUMIF To find John M.’s total sales, we use the following formula for cell C20:To find John M.’s total sales, we use the following formula for cell C20: =SUMIF(A7:A16, "John M.", C7:C16) This says to sum the elements of C7:C16 where the corresponding element of A7:A16 equals “John M.”This says to sum the elements of C7:C16 where the corresponding element of A7:A16 equals “John M.”
7 Using COUNTIF To get the average sale for John M., we need to divide by his number of sales. We can do this using COUNTIF in the formula for cell C21:To get the average sale for John M., we need to divide by his number of sales. We can do this using COUNTIF in the formula for cell C21: =C20/COUNTIF(A7:A16, "John M.") COUNTIF(A7:A16, “John M.”) returns the number of entries in the given range that equal John M.COUNTIF(A7:A16, “John M.”) returns the number of entries in the given range that equal John M. Note they must be exactly the same!Note they must be 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
8 Sales per Neighborhood We can use the same idea to get average and total sales per neighborhood. This is done in C22 and C23 for Laurelhurst using the following formulas:We can use the same idea to get average and total sales per neighborhood. This is done in C22 and C23 for Laurelhurst using the following formulas: For C22: = SUMIF(B7:B16,"Laurelhurst",C7:C16)For C22: = SUMIF(B7:B16,"Laurelhurst",C7:C16) For C23: = C22/COUNTIF(B7:B16,"Laurelhurst")For C23: = C22/COUNTIF(B7:B16,"Laurelhurst")
9 A More Complex Condition Typical complex conditions may have the AND of two or more conditionsTypical complex conditions may have the AND of two or more conditions Excel has a built-in way to handle this using COUNTIFS and SUMIFSExcel has a built-in way to handle this using COUNTIFS and SUMIFS Let’s look at how to figure out John M.’s total and average sales in LaurelhurstLet’s look at how to figure out John M.’s total and average sales in Laurelhurst That is, we want to count sales that are by John M. provided it is in LaurelhurstThat is, we want to count sales that are by John M. provided it is in Laurelhurst
10 Using COUNTIFS and SUMIFS John M.’s Laurelhurst Total (C24): =SUMIFS(C7:C16, A7:A16, "John M.", B7:B16, "Laurelhurst") Note the column to be summed comes first, followed by the other columns and their conditions The equality test = is implied And the Boolean AND condition for both being true is implied John M.’s Laurelhurst Average (C25): =C24/COUNTIFS(A7:A16,"John M.", B7:B16, "Laurelhurst")
11 An OR Condition Suppose we want a condition that is an OR instead of an AND, or uses a comparison other than =Suppose we want a condition that is an OR instead of an AND, or uses a comparison other than = One way to do it is to make a column for the condition and then use the column values in a SUMIF or COUNTIFOne way to do it is to make a column for the condition and then use the column values in a SUMIF or COUNTIF For example, let’s say we want the total sales for houses in Irvington as well as in AlamedaFor example, let’s say we want the total sales for houses in Irvington as well as in Alameda Logically, this means the Neighborhood field is Irvington OR it is AlamedaLogically, this means the Neighborhood field is Irvington OR it is Alameda
12 A New Column for the Condition Column E on the spreadsheet is the result of testing whether the neighborhood equals Irvington OR Alameda The formula looks like this: =IF(OR(B7="Alameda",B7="Irvington"),"yes","no") Note the format for the IF: IF(condition, true branch, false branch) These IF’s can be nested. But you can see that too much nesting would be hard to read
13 Using the Condition C26 has the total sales for properties in Irvington and Alameda Here is the formula, using Column E: =SUMIF(E7:E16,"yes",C7:C16)
14 Conditional Formatting Along with conditionals in formulas, Excel also lets you specify formatting using conditionsAlong with conditionals in formulas, Excel also lets you specify formatting using conditions There is a conditional formatting button in the main ribbon, shown on the next slideThere is a conditional formatting button in the main ribbon, shown on the next slide (We did this on the windows version but it is very similar on the Mac)(We did this on the windows version but it is very similar on the Mac)
15
16 Data Bars Column C shows an example of Data Bars formatting The colored bar in each cell is determined by the cell value, with the smallest cell having only a small colored area and the largest one almost filled
17 Using Formulas Column A shows an example of using a formula in conditional formatting The formula must have a Boolean value In this case I colored the cell if the sale was in Laurelhurst and above the average price for Laurelhurst Here is the formula: =AND(C7>$C$23, B7="Laurelhurst")
18 If you make a lot of formatting rules… 18 The Manage Rules item lets you edit and examine your rules
19 Try it Yourself! Knowing how to use conditionals makes Excel even more powerful You should play around with the example spreadsheet or one that you make yourself to understand the concepts