11.08.2013 Views

Excel's Formula - sisman

Excel's Formula - sisman

Excel's Formula - sisman

SHOW MORE
SHOW LESS

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

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

618<br />

Part VI: Developing Custom Worksheet Functions<br />

You can use mixed case. VBA does not distinguish between cases. To make a function<br />

name more readable, you can use InterestRate rather than interestrate.<br />

You can’t use spaces or periods. Many of Excel’s built-in functions include a period character,<br />

but that character is not allowed in VBA function names. To make function names<br />

more readable, you can use the underscore character (Interest_Rate).<br />

You can’t embed the following characters in a function’s name: #, $, %, &, or !. These are<br />

type declaration characters that have a special meaning in VBA.<br />

You can use a function name with as many as 255 characters. However, shorter names<br />

are usually more readable and easier to work with.<br />

Using functions in formulas<br />

Using a custom VBA function in a worksheet formula is like using a built-in worksheet function.<br />

However, you must ensure that Excel can locate the Function procedure. If the Function procedure<br />

is in the same workbook as the formula, you don’t have to do anything special. If it’s in a<br />

different workbook, you may have to tell Excel where to find it. You can do so in three ways:<br />

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

function called CountNames that’s defined in a workbook named Myfuncs.xlsm, you<br />

can use a formula like the following:<br />

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

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

inserted automatically.<br />

Set up a reference to the workbook. You do this with the VB Editor’s Tools➜References<br />

command (see Figure 23-2). If the function is defined in a referenced workbook, you<br />

don’t need to use the worksheet name. Even when the dependent workbook is assigned<br />

as a reference, the Insert Function dialog box continues to insert the workbook reference<br />

(even though it’s not necessary).<br />

Function names in a referenced workbook do not appear in the <strong>Formula</strong> AutoComplete<br />

drop-down list. <strong>Formula</strong> AutoComplete works only when the formula is entered into the<br />

workbook that contains the custom function, or when it is contained in an installed add-in.<br />

By default, all projects are named VBAProject — and that’s the name that appears in the<br />

Available References list in the References dialog box. To make sure that you select<br />

the correct project in the References dialog box, keep your eye on the bottom of the<br />

dialog box, which shows the path and filename for the selected item. Better yet, change<br />

the name of the project to be more descriptive. To change the name, select the project,<br />

press F4 to display the Properties window, and then change the Name property to<br />

something other than VBAProject. Use a unique name because Excel does not let you<br />

create two references with the same name.

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

Saved successfully!

Ooh no, something went wrong!