18.11.2014 Views

Microsoft Office

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Performing Magic with Array Formulas 18<br />

To return the first invalid item in MyList, use the following array formula:<br />

{=INDEX(MyList,MATCH(TRUE,ISNA(MATCH(MyList,Master,0)),0))}<br />

Summing the digits of an integer<br />

I can’t think of any practical application for the example in this section, but it’s a good demonstration of the<br />

power of an array formula. The following array formula calculates the sum of the digits in a positive integer,<br />

which is stored in cell A1. For example, if cell A1 contains the value 409, the formula returns 13 (the sum<br />

of 4, 0, and 9).<br />

{=SUM(MID(A1,ROW(INDIRECT(“1:”&LEN(A1))),1)*1)}<br />

To understand how this formula works, start with the ROW function, as shown here:<br />

{=ROW(INDIRECT(“1:”&LEN(A1)))}<br />

This function returns an array of consecutive integers beginning with 1 and ending with the number of digits<br />

in the value in cell A1. For example, if cell A1 contains the value 409, the LEN function returns 3, and<br />

the array generated by the ROW functions is<br />

{1,2,3}<br />

CROSS-REF<br />

For more information about using the INDIRECT function to return this array, see Chapter 17.<br />

This array is then used as the second argument for the MID function. The MID part of the formula, simplified<br />

a bit and expressed as values, is the following:<br />

{=MID(409,{1,2,3},1)*1}<br />

This function generates an array with three elements:<br />

{4,0,9}<br />

By simplifying again and adding the SUM function, the formula looks like this:<br />

{=SUM({4,0,9})}<br />

This formula produces the result of 13.<br />

The values in the array created by the MID function are multiplied by 1 because the MID function<br />

returns a string. Multiplying by 1 forces a numeric value result. Alternatively, you can use<br />

the VALUE function to force a numeric string to become a numeric value.<br />

NOTE<br />

Notice that the formula doesn’t work with a negative value because the negative sign is not a numeric value.<br />

The following formula solves this problem by using the ABS function to return the absolute value of the<br />

number. Figure 18.4 shows a worksheet that uses this formula in cell B4.<br />

{=SUM(VALUE(MID(ABS(A4),ROW(INDIRECT(“1:”&LEN(ABS(A4)))),1)))}<br />

The formula was copied down to calculate the sum of the digits for other values in column A.<br />

339

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

Saved successfully!

Ooh no, something went wrong!