Excel VBA Tutorial Part 4 - VBA Functions & Subroutines
Built-In VBA Functions
Excel VBA has a large number of built-in functions that you can use in your macros.
For a list of these:
In Excel Visual Basic, a set of commands to perform a specific task is placed into a procedure, which can be a function or a subroutine. The main difference between a VBA function and a VBA subroutine is that a function returns a result, whereas a subroutine does not. Therefore, if you wish to perform a task that returns a result (eg. summing of a group of numbers), you will generally use a function, but if you just need a set of actions to be carried out (eg. formatting a set of cells), you might choose to use a subroutine. ArgumentsIn VBA, both functions and subroutines can be passed data via arguments, which are declared in the VBA function or subroutine definition. For example, we might have a VBA subroutine that adds an Integer to every cell in the current range. You could supply the value of the integer to the subroutine via an argument, as follows:
Optional ArgumentsYou can also define VBA functions or subroutines to have Optional arguments. These are arguments that the user can supply if they want, but if they are omitted, the procedure will still work. To return to the example above, if we wanted to make the supplied integer optional, this would be declared as follows:
Sub AddToCells(Optional i As Integer)
Of course this needs to be considered in the subroutine code. We need to tell the subroutine to check if the argument i has been supplied, before attempting to use it. You can check if an argument has been supplied to a procedure, using the IsMissing command, as follows:
VBA allows you to use multiple Optional arguments in a procedure, as long as the Optional arguments are positioned at the end of the argument list. Passing Arguments By Value and By ReferenceWhen arguments are passed to VBA functions, they can be passed in two ways:
We can specify whether an argument is passed to a VBA function by value or by reference by using the ByVal or the ByRef keyword when defining the procedure. This is shown below:
It should be noted that, by default, in VBA, arguments are passed by Reference, so if you do not use the ByVal or the ByRef keyword, the arguments will be passed by Reference. Before discussing further properties of VBA Functions and Subroutines, it is useful to look at the two types of procedure individually. The following two sections provide a brief discussion of VBA Functions and VBA Subroutines, along with simple examples : VBA FunctionsThe VBA editor recognises a function, because the commands are positioned between the following start and end commands:
As previously mentioned, VBA functions (unlike subroutines) return a value. The return values have the following rules:
This is best illustrated by the example below. VBA Function Example: Perform Mathematical Operations on 3 NumbersThe following code shows an example of a simple VBA function that receives three arguments, each of which are 'Doubles' (double precision floating point numbers). The function returns a further 'Double', which is the sum of the first two arguments, minus the third argument :
The above very simple VBA function illustrates the way in which data arguments are supplied to a function, and the way the return type is defined as being a 'Double' (ie. by the term "As Double" which is included after the function arguments). The above example also shows how the function result is stored in a variable that has the same name as the function. Calling VBA FunctionsIf the above simple function is typed into a Module in the Visual Basic Editor, it will then be available to be used in the worksheets of your Excel workbook, in the same way as Excel's Built-In Functions. Therefore if cells A1, A2 and A3 contain numeric values, you could call the SumMinus function by typing the following into any cell:
=SumMinus(A1, A2, A3)
VBA SubroutinesThe VBA editor recognises a Subroutine, because the commands are positioned between the following start and end commands:
VBA Subroutine Example 1: Center and Apply Font Size to a Selected Range of CellsThe following code shows an example of a simple VBA subroutine that applies formatting to the current selected cell range. The cells are formatted to be aligned centrally (both horizontally and vertically) and to have a user-supplied font size :
The above example illustrates how subroutines perform actions but do not return values. This example also includes the Optional argument, iFontSize. If iFontSize is not supplied to the subroutine, then the current range's font size is not changed. However, if iFontSize is supplied to the function, then the current range is set to have the supplied font size. VBA Subroutine Example 2: Center and Apply Bold Font to a Selected Range of CellsThe following code is similar to example 1, but instead of supplying a font size to the selected range, the cells are set to have a bold font. This example has been included to show a subroutine that does not receive any arguments :
Calling Excel VBA SubroutinesSubroutines cannot be typed directly into a cell in Excel, in the same way that VBA functions can, because subroutines don't return a value. However, provided they have no arguments (and are Public - see below), Excel VBA subroutines are available to the user of a spreadsheet. Therefore, if the above simple subroutines are typed into a Module in the Visual Basic Editor, Example 2 will then be available to be used in the worksheets of your Excel workbook but Example 1 will not (as it has an argument). For those subroutines that are accessible from the workbook, a simple way to run (or execute) the subroutine is :
Alternatively, you can assign a key combination to your subroutine, to enable quick and easy execution of the code. To do this:
WARNING: When assigning a key combination to a macro, take care not to select one of Excel's predefined key combinations (eg. CTRL-C). If you do select an existing Excel key combination, this will be overwritten by your macro, and you, or other users, may end up accidentally executing your macro code! Scope of VBA Functions & SubroutinesIn Part 2 of this tutorial, we discussed the scope of variables and constants and the role of the Public and Private keywords. These keywords have the same meaning when applied to VBA Functions and Subroutines :
It should be noted that if no keyword is inserted at the start of a VBA function or subroutine declaration, then the default is for the procedure to be Public (ie. to be accessible from anywhere in the VBA Project. (This is different to variable declarations, which are Private by default). Early Exit From VBA Functions & SubroutinesIf you want to exit a VBA function or subroutine before its natural end point, you can do this using the Exit Function or the Exit Sub command. This is illustrated below, in a simple function that expects to receive a positive value to work with. If the value received is not positive, the function cannot continue, so it highlights the error to the user and exits the function immediately:
|
|||||||||||||||||||||
|
|
|||||||||||||||||||||
Copyright © 2008-2010 ExcelFunctions.net |