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.

714<br />

Part VI: Developing Custom Worksheet Functions<br />

The Excel SUM function considers a text string to have a value of 0 unless it appears as a<br />

literal argument (that is, as an actual value, not a variable). Therefore, MYSUM adds the<br />

cell’s value only if it can be evaluated as a number (VBA’s IsNumeric function is used<br />

for this).<br />

Dealing with Boolean arguments is tricky. For MYSUM to emulate SUM exactly, it needs to<br />

test for a literal TRUE in the argument list and compensate for the difference (that is, add<br />

2 to –1 to get 1).<br />

For range arguments, the function uses the Intersect method to create a temporary<br />

range that consists of the intersection of the range and the sheet’s used range. This handles<br />

cases in which a range argument consists of a complete row or column, which would<br />

take forever to evaluate.<br />

You may be curious about the relative speeds of SUM and MYSUM. MYSUM, of course, is much<br />

slower, but just how much slower depends on the speed of your system and the formulas themselves.<br />

On my system, a worksheet with 5,000 SUM formulas recalculated instantly. After I<br />

replaced the SUM functions with MYSUM functions, it took about 8 seconds. MYSUM may be<br />

improved a bit, but it can never come close to SUM’s speed.<br />

By the way, I hope you understand that the point of this example is not to create a new SUM<br />

function. Rather, it demonstrates how to create custom worksheet functions that look and work<br />

like those built into Excel.

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

Saved successfully!

Ooh no, something went wrong!