|
|
|
|
Custom FunctionsOne of the easiest ways to take advantage of VBA in Excel is to write custom functions. Excel has a large number of built-in functions that you can use in spreadsheet formulas. Examples include:
In general, a function takes one or more objects (values, ranges, etc.) as input and returns a single object (typically a value). The things that are sent to a function as input are called arguments and the thing that is returned by a function is often called the return value. In some cases, we may encounter situations where we need a function to do something but the function is not provided by Excel. We can easily fix this problem by creating a custom function in VBA. FormatThe basic format for a custom function is as follows:
The Public statement is optional. This means that the function can be called by VB code outside the module where the function is declared and from Excel formulas in cells. If you omit the Public statement, the function is public by default (as opposed to Private). The function_name is the name you provide to the function. This can be any name you want, as long as you follow the same rules we use for defining VB variable names. The args are the arguments to the function. You can have any number of arguments. The arguments are listed in the same way you declare variables, except that you omit the Dim part. The args list serves two purposes: 1) it defines how many arguments are used, and 2) it defines the type for each argument. The following are some sample argument lists:
The Type part defines the type of object returned by the function. Typical examples are Double, Integer, String, and Boolean. Somewhere in the code, you must have line where you set the function name equal to a value. You should think of the function name as a variable. You must store the value returned by the function in the variable at some point before you hit the End Function statement. There is one more important point, whenever you create a function that you want to use in an Excel formula, it should always be placed in a module under the Modules folder in the VBE. ExamplesNow let's look at some examples. The following function takes two numbers as arguments and returns the minimum of the two numbers. This basically duplicates the Min function provided by Excel, but it serves as a useful example:
Once this function is created, you can then use it in one of your Excel formulas as follows:
Now let's look at something a little more complicated. In many cases, we want our function to use a cell range as one of the arguments. The following function returns the number of negative values in a range:
The function could then be called from an Excel formula as follows:
The next function takes two arguments: a range and an integer n. It computes the sum of values in the range minus the lowest n values. This function takes advantage of the standard Excel functions Sum and Small. The Small function returns the nth lowest value in a range.
This function could then be used in an Excel formula as follows:
Functions in VBFinally, it should be noted that you can call custom functions from other places in your VB code as well as from Excel formulas. For example, you could use the my_min function defined above as follows:
|