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.

Creating Custom Worksheet Functions 40<br />

Using custom functions in a worksheet formula<br />

You use a custom function in a worksheet formula just like you use built-in functions. However, you must<br />

ensure that Excel can locate the function. If the function procedure is in the same workbook, you don’t have<br />

to do anything special. If the function is defined in a different workbook, you may have to tell Excel where<br />

to find the function. The following are the three ways in which you can do this:<br />

n<br />

n<br />

n<br />

Precede the function’s name with a file reference. For example, if you want to use a function<br />

called CountNames that’s defined in a workbook named MyFunctions, you can use a reference<br />

such as the following:<br />

=MyFunctions.xlsm!CountNames(A1:A1000)<br />

If the workbook name contains a space, you need to add single quotes around the workbook<br />

name. For example:<br />

=’My Functions.xlsm’!CountNames(A1:A1000)<br />

If you insert the function with the Insert Function dialog box, the workbook reference is inserted<br />

automatically.<br />

Set up a reference to the workbook. If the custom function is defined in a referenced workbook,<br />

you don’t need to precede the function name with the workbook name. You establish a reference<br />

to another workbook with the Tools ➪ References command (in the VB Editor). You are presented<br />

with a list of references that includes all open workbooks. Place a check mark in the item<br />

that refers to the workbook that contains the custom function. (Use the Browse button if the<br />

workbook isn’t open.)<br />

Create an add-in. When you create an add-in from a workbook that has function procedures,<br />

you don’t need to use the file reference when you use one of the functions in a formula; the addin<br />

must be installed, however. Chapter 45 discusses add-ins.<br />

Your Function procedures don’t appear in the Macro dialog box because you can’t execute a<br />

function directly. As a result, you need to do extra, up-front work to test your functions as<br />

you’re developing them. One approach is to set up a simple Sub procedure that calls the function. If the<br />

function is designed to be used in worksheet formulas, you can enter a simple formula to test it as you’re<br />

developing the function.<br />

NOTE<br />

Function Procedure Arguments<br />

Keep in mind the following about function procedure arguments:<br />

n<br />

n<br />

Arguments can be variables (including arrays), constants, literals, or expressions.<br />

Some functions do not have arguments.<br />

n Some functions have a fixed number of required arguments (from 1 to 60).<br />

n Some functions have a combination of required and optional arguments.<br />

The following sections present a series of examples that demonstrate how to use arguments effectively with<br />

functions. Coverage of optional arguments is beyond the scope of this book.<br />

ON the CD-ROM<br />

The examples in this chapter are available on the companion CD-ROM. The file is named VBA<br />

functions.xlsm.<br />

707

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

Saved successfully!

Ooh no, something went wrong!