30.04.2017 Views

4523756273

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

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

Chapter 20: Creating Worksheet Functions — and Living to Tell about It<br />

343<br />

argument. The LBound and UBound functions determine the beginning and<br />

ending elements of the array. The beginning element is normally 0 unless you<br />

either declare it as something else or use an Option Base 1 statement at the<br />

beginning of your module.<br />

ParamArray can apply to only the last argument in the procedure. It is always<br />

a Variant data type, and it is always an optional argument (although you don’t<br />

use the Optional keyword). Figure 20-2 shows this function in use. Examine the<br />

figure to see how the results differ from those produced by the Excel<br />

CONCATENATE function, which doesn’t insert a space between the concatenated<br />

items.<br />

Figure 20-2:<br />

Using the<br />

Concat<br />

function in<br />

worksheet<br />

formulas.<br />

Functions That Return an Array<br />

Array formulas are one of Excel’s most powerful features. If you’re familiar<br />

with array formulas, you’ll be happy to know that you can create VBA functions<br />

that return an array.<br />

Returning an array of month names<br />

I’ll start out with a simple example. The MonthNames function returns a<br />

12-element array of — you guessed it — month names.<br />

Function MonthNames()<br />

MonthNames = Array(“January”, “February”, “March”, _<br />

“April”, “May”, “June”, “July”, “August”, _<br />

“September”, “October”, “November”, “December”)<br />

End Function<br />

To use the MonthNames function in a worksheet, you must enter it<br />

as a 12-cell array formula. For example, select range A1:L1 and enter<br />

=MonthNames(). Then use Ctrl+Shift+Enter to enter the array formula in all<br />

12 selected cells. Figure 20-3 shows the result.

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

Saved successfully!

Ooh no, something went wrong!