18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Performing Magic with Array Formulas 18<br />

You also can eliminate these types of rounding errors by using the ROUND function in the formula that calculates<br />

each row total in column E (which does not require an array formula).<br />

Summing every nth value in a range<br />

Suppose that you have a range of values and you want to compute the sum of every third value in the list —<br />

the first, the fourth, the seventh, and so on. One solution is to hard-code the cell addresses in a formula.<br />

But a better solution is to use an array formula.<br />

NOTE<br />

In Figure 18.6, the values are stored in a range named Data, and the value of n is in cell D2<br />

(named n).<br />

FIGURE 18.6<br />

An array formula returns the sum of every nth value in the range.<br />

The following array formula returns the sum of every nth value in the range:<br />

{SUM(IF(MOD(ROW(INDIRECT(“1:”&COUNT(Data)))-1,n)=0,Data,””))}<br />

This formula returns 70, which is the sum of every third value in the range.<br />

This formula generates an array of consecutive integers, and the MOD function uses this array as its first<br />

argument. The second argument for the MOD function is the value of n. The MOD function creates another<br />

array that consists of the remainders when each row number is divided by n. When the array item is 0 (that<br />

is, the row is evenly divisible by n), the corresponding item in the Data range will be included in the sum.<br />

You find that this formula fails when n is 0 (that is, when it sums no items). The modified array formula<br />

that follows uses an IF function to handle this case:<br />

{=IF(n=0,0,SUM(IF(MOD(ROW(INDIRECT(“1:”&COUNT(data)))-<br />

1,n)=0,data,””)))}<br />

341

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!