11.08.2013 Views

Excel's Formula - sisman

Excel's Formula - sisman

Excel's Formula - sisman

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Chapter 15: Performing Magic with Array <strong>Formula</strong>s 399<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<br />

of the power of an array formula. The following array formula calculates the sum of the<br />

digits in a positive integer, which is stored in cell A1. For example, if cell A1 contains the value<br />

409, the formula returns 13 (the sum 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<br />

of digits in the value in cell A1. For example, if cell A1 contains the value 409, the LEN function<br />

returns 3, and the array generated by the ROW functions is<br />

{1,2,3}<br />

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

Chapter 14.<br />

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

simplified 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<br />

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

you can use the VALUE function to force a numeric string to become a numeric value.

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

Saved successfully!

Ooh no, something went wrong!