Microsoft Office Tutorials and References
In Depth Information
About Function Procedures
h You can use mixed case. VBA does not distinguish between cases. To make a function
name more readable, you can use InterestRate rather than interestrate .
h You can’t use spaces or periods. Many of Excel’s built-in functions include a period
character, but that character is not allowed in VBA function names. To make function names
more readable, you can use the underscore character ( Interest_Rate ).
h You can’t embed the following characters in a function’s name: #, $, %, &, or !. These are
type declaration characters that have a special meaning in VBA.
h You can use a function name with as many as 255 characters. However, shorter names
are usually more readable and easier to work with.
Using functions in formulas
Using a custom VBA function in a worksheet formula is like using a built-in worksheet function.
However, you must ensure that Excel can locate the Function procedure. If the Function
procedure is in the same workbook as the formula, you don’t have to do anything special. If it’s in a
different workbook, you may have to tell Excel where to find it. You can do so in three ways:
h Precede the function’s name with a file reference For example, if you want to use a .
function called CountNames that’s defined in a workbook named Myfuncs.xlsm , you
can use a formula like the following:
=Myfuncs.xlsm!CountNames(A1:A1000)
If you insert the function with the Insert Function dialog box, the workbook reference is
inserted automatically.
h Set up a reference to the workbook You do this with the VB Editor’s Tools .
References
command (see Figure 23-2). If the function is defined in a referenced workbook, you
don’t need to use the worksheet name. Even when the dependent workbook is assigned
as a reference, the Insert Function dialog box continues to insert the workbook reference
(even though it’s not necessary).
Function names in a referenced workbook do not appear in the Formula AutoComplete
drop-down list. Formula AutoComplete works only when the formula is entered into the
workbook that contains the custom function, or when it is contained in an installed add-in.
By default, all projects are named VBAProject — and that’s the name that appears in the
Available References list in the References dialog box. To make sure that you select
the correct project in the References dialog box, keep your eye on the bottom of the
dialog box, which shows the path and filename for the selected item. Better yet, change
the name of the project to be more descriptive. To change the name, select the project,
press F4 to display the Properties window, and then change the Name property to
something other than VBAProject. Use a unique name because Excel does not let you
create two references with the same name.
 
Search JabSto ::




Custom Search