Microsoft Office Tutorials and References
In Depth Information
Function Procedures
Function Procedures
A custom Function procedure has much in common with a Sub procedure. (For more
information on Sub procedures, see Chapter 9.)
The syntax for declaring a function is as follows:
[Public | Private][Static] Function name ([arglist])[As type]
[name = expression]
[Exit Function]
[name = expression]
End Function
The Function procedure contains the following elements:
h Public : (Optional) Indicates that the Function procedure is accessible to all other
procedures in all other modules in all active Excel VBA projects.
h Private : (Optional) Indicates that the Function procedure is accessible only to other
procedures in the same module.
h Static : (Optional) Indicates that the values of variables declared in the Function
procedure are preserved between calls.
h Function : (Required) Indicates the beginning of a procedure that returns a value or
other data.
h name : (Required) Represents any valid Function procedure name, which must follow
the same rules as a variable name.
h arglist : (Optional) Represents a list of one or more variables that represent arguments
passed to the Function procedure. The arguments are enclosed in parentheses. Use a
comma to separate pairs of arguments.
h type : (Optional) Is the data type returned by the Function procedure.
h instructions : (Optional) Are any number of valid VBA instructions.
h Exit Function : (Optional) Is a statement that forces an immediate exit from the
Function procedure prior to its completion.
h End Function : (Required) Is a keyword that indicates the end of the Function
A key point to remember about a custom function written in VBA is that a value is always assigned
to the function’s name a minimum of one time, generally when it has completed execution.
Search JabSto ::

Custom Search