|
|
| Back to Excel Homepage | Excel VBA - Reference Guide |
|
Menu Procedure types
Other links
|
There are three types of procedures:
1. Sub - Standard sub routine 2. Function - a routine that returns an answer 3. Property - reserved for Class Modules The third item is not discussed in this topic as it is deemed advanced VBA. Sub ProcedureThis is the most commonly used procedure that a recorded and edited macro typically uses. It executes code line by line in order, carrying out a series of actions and/or calculations. The signature for this type of procedure is:
The ‘Arguments’ element is optional which can be explicit or implicit. This allows values and /or references to be passed into the calling procedure and handled as a variable. When recording a macro, no arguments are used and the parenthesis for the named procedure remains empty. If you create a procedure intended as a macro in Excel, users must not specify any arguments. Sub procedures can be recursive meaning that branching to another procedure is permitted which then returns back to the main calling procedure. Calling another procedure can include the Call statement followed by the name of the procedure with optional arguments. If arguments are used, users must use parenthesis around the argument list. Example of the CALL statement
(Click here for an understanding of the MsgBox statement)
Correct
Incorrect - must use the parenthesis
Correct (alternative) - No Call keyword used & no parenthesis therefore required.
A procedure can be prematurely terminated, placed before the ‘End Sub’ statement by using the ‘Exit Sub’ statement.
Function ProcedureThe main difference between a Sub and Function procedure is that a Function procedure carries out a procedure and will return an answer whereas a Sub procedure carries out the procedure without an answer. A simple analogy of a Function procedure compared to that of a Sub procedure could be illustrated using two example features of Excel:
The signature for this type of procedure is:
The Arguments element is optional which can be explicit or implicit. This allows values and /or references to be passed into the calling procedure and handled as a variable. The optional Type attribute can be used to make the function explicit. Without a type declared, the function is implicit (As Variant). The last line before the End Function signature uses the name of the procedure to return the expression (or answer) of the function. Users cannot define a function inside another function, sub or even property procedures. This type of procedure can be called in a module by a Sub procedure or executed as a user defined function on a worksheet in Excel. A procedure can be prematurely terminated, placed
before the End Function statement by using the Exit
Function statement. This acts and responds in the same way as
described in the previous section (Sub Procedures). An example of a Function procedure:
A Sub procedure that uses of the above function:
In Excel, this function can also be used (known as a User Defined Function - UDF)
Click on this link for more information on user defined functions.
|
VBA Keywords: MsgBox, Call, Exit Sub, Exit Function. |
|
© copyright 2010 TP Development & Consultancy Ltd, All Rights Reserved. All trademarks are copyrighted by their respective owners. Please read our terms of use and privacy policy. |