Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Excel_2010_Bible.pdf
Скачиваний:
26
Добавлен:
13.03.2015
Размер:
11.18 Mб
Скачать

Chapter 13: Creating Formulas That Count and Sum

Cross-Reference

See Chapter 16 for more information about using array constants. n

Conditional Sums Using a Single Criterion

Often, you need to calculate a conditional sum. With a conditional sum, values in a range that meet one or more conditions are included in the sum. This section presents examples of conditional summing by using a single criterion.

The SUMIF function is very useful for single-criterion sum formulas. The SUMIF function takes three arguments:

range: The range containing the values that determine whether to include a particular cell in the sum.

criteria: An expression that determines whether to include a particular cell in the sum.

sum_range: Optional. The range that contains the cells you want to sum. If you omit this argument, the function uses the range specified in the first argument.

The examples that follow demonstrate the use of the SUMIF function. These formulas are based on the worksheet shown in Figure 13.14, set up to track invoices. Column F contains a formula that subtracts the date in column E from the date in column D. A negative number in column F indicates a past-due payment. The worksheet uses named ranges that correspond to the labels in row 1.

On the CD

All the examples in this section also appear on the companion CD-ROM. The file is named conditional sum.xlsx.

FIGURE 13.14

A negative value in Column F indicates a past-due payment.

303

Part II: Working with Formulas and Functions

Summing only negative values

The following formula returns the sum of the negative values in column F. In other words, it returns the total number of past-due days for all invoices. For this worksheet, the formula returns –63.

=SUMIF(Difference,”<0”)

Because you omit the third argument, the second argument (“<0”) applies to the values in the

Difference range.

You don’t need to hard-code the arguments for the SUMIF function into your formula. For example, you can create a formula, such as the following, which gets the criteria argument from the contents of cell G2:

=SUMIF(Difference,G2)

This formula returns a new result if you change the criteria in cell G2.

Summing values based on a different range

The following formula returns the sum of the past-due invoice amounts (in column C):

=SUMIF(Difference,”<0”,Amount)

This formula uses the values in the Difference range to determine whether the corresponding values in the Amount range contribute to the sum.

Summing values based on a text comparison

The following formula returns the total invoice amounts for the Oregon office:

=SUMIF(Office,”=Oregon”,Amount)

Using the equal sign in the argument is optional. The following formula has the same result:

=SUMIF(Office,”Oregon”,Amount)

To sum the invoice amounts for all offices except Oregon, use this formula:

=SUMIF(Office,”<>Oregon”,Amount)

304

Chapter 13: Creating Formulas That Count and Sum

Summing values based on a date comparison

The following formula returns the total invoice amounts that have a due date after May 1, 2010:

=SUMIF(DateDue,”>=”&DATE(2010,5,1),Amount)

Notice that the second argument for the SUMIF function is an expression. The expression uses the DATE function, which returns a date. Also, the comparison operator, enclosed in quotes, is concatenated (using the & operator) with the result of the DATE function.

The formula that follows returns the total invoice amounts that have a future due date (including today):

=SUMIF(DateDue,”>=”&TODAY(),Amount)

Conditional Sums Using Multiple Criteria

The examples in the preceding section all used a single comparison criterion. The examples in this section involve summing cells based on multiple criteria.

Figure 13.15 shows the sample worksheet again, for your reference. The worksheet also shows the result of several formulas that demonstrate summing by using multiple criteria.

Using And criteria

Suppose that you want to get a sum of the invoice amounts that are past due and associated with the Oregon office. In other words, the value in the Amount range will be summed only if both of the following criteria are met:

The corresponding value in the Difference range is negative.

The corresponding text in the Office range is Oregon.

If the worksheet won’t be used by anyone running a version prior to Excel 2007, the following formula does the job:

=SUMIFS(Amount,Difference,”<0”,Office,”Oregon”)

The array formula that follows returns the same result and will work in all versions of Excel.

{=SUM((Difference<0)*(Office=”Oregon”)*Amount)}

305

Part II: Working with Formulas and Functions

FIGURE 13.15

This worksheet demonstrates summing based on multiple criteria.

Using Or criteria

Suppose that you want to get a sum of past-due invoice amounts or ones associated with the Oregon office. In other words, the value in the Amount range will be summed if either of the following criteria is met:

The corresponding value in the Difference range is negative.

The corresponding text in the Office range is Oregon.

This example requires an array formula:

{=SUM(IF((Office=”Oregon”)+(Difference<0),1,0)*Amount)}

A plus sign (+) joins the conditions; you can include more than two conditions.

306

Chapter 13: Creating Formulas That Count and Sum

Using And and Or criteria

As you may expect, things get a bit tricky when your criteria consists of both And and Or operations. For example, you may want to sum the values in the Amount range when both of the following conditions are met:

The corresponding value in the Difference range is negative.

The corresponding text in the Office range is Oregon or California.

Notice that the second condition actually consists of two conditions joined with Or. The following array formula does the trick:

{=SUM((Difference<0)*IF((Office=”Oregon”)+

(Office=”California”),1)*Amount)}

307

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]