You probably put the function in the code module for a Sheet (for example, Sheet1 ) or in the ThisWorkbook module. Custom worksheet functions must reside in standard VBA modules.
VBA functions called from a worksheet formula have some limitations. In general, they must be strictly passive . That is, they can't change the active cell , apply formatting, open workbooks, or change the active sheet. If the function attempts to do any of these things, the formula will return an error.
Functions can only perform calculations and return a value. An exception to this rule is the VBA MsgBox function. A custom function can display a message box whenever it is recalculated. This is very handy for debugging a custom function.
To add a description for your custom function, activate the workbook that contains the Function procedure. Then choose View Macros Macros to display the Macro dialog box. Your function won't be listed, so you must type it into the Macro Name box. After typing the function's name, click Options to display the Macro Options dialog box. Enter the descriptive text in the Description box.
Unfortunately, no.
You need to use VBA to do this. The following instruction assigns the function named MyFunc to Category 1 (Financial):
Application.MacroOptions Macro:="MyFunc", Category:=1
The following table lists the valid function category numbers :
Number | Category |
---|---|
| No category (appears only in All) |
1 | Financial |
2 | Date & Time |
3 | Math & Trig |
4 | Statistical |
5 | Lookup & Reference |
6 | Database |
7 | Text |
8 | Logical |
9 | Information |
10 | Commands (normally hidden) |
11 | Customizing (normally hidden) |
12 | Macro Control (normally hidden) |
13 | DDE/External (normally hidden) |
14 | User Defined (default) |
15 | Engineering |
You can create a new function category by using an XLM macro. However, this method is not reliable and is not recommended.
If your function is named MyFunction , you can use the following instruction to return an error value to the cell that contains the function:
MyFunction = CVErr(xlErrValue)
In this example, xlErrValue is a predefined constant. Constants for the other error values are listed in the Help system.
Press Ctrl+Alt+F9.
In most cases, yes. Excel's worksheet functions are accessed via the WorksheetFunction method of the Application object. For example, you could access the SUM worksheet function with a statement such as the following:
Ans = Application.WorksheetFunction.Sum(Range("A1:A3"))
This example assigns the sum of the values in A1:A3 (on the active sheet) to the Ans variable.
Generally, if VBA includes an equivalent function, you cannot use Excel's worksheet version. For example, because VBA has a function to compute square roots ( Sqr ), you can't use the SQRT worksheet function in your VBA code.
Use a carriage return or a linefeed character to force a new line. The following statement displays the message box text on two lines. vbNewLine is a built-in constant that represents a carriage return.
MsgBox "Hello" & vbNewLine & Application.UserName