Page 125 - Microsoft Office Excel 2003 Programming Inside Out
P. 125
Creating Sub and Function Procedures
The SUM function adds everything in the statement that calls it; if it can’t make sense of its
input, such as when it tries to add a non-numerical value, it will return an error message and
display an error code. If a Function procedure has no arguments, its Function statement must
include an empty set of parentheses.
[Private | Public] [Static] Function name [(arglist)] [As type]
[statements]
[name = expression]
[Exit Function]
[statements]
[name = expression]
End Function
Table 5-2 describes the elements of a Function procedure, many of which will be familiar
from the description of a Sub procedure.
Table 5-2. Elements of a Function Procedure
Element Description
Public An optional element that indicates the Function procedure is
accessible to all other procedures in all modules. If used in a module
that contains an Option Private statement, the procedure is not
available outside the project.
Private An optional element that indicates the Function procedure is
accessible only to other procedures in the module where it is
declared.
Static An optional element that indicates the Function procedure’s local
variables are preserved between calls. The Static attribute doesn’t
affect variables that are declared outside the Function procedure,
even if they are used in the procedure.
name A required element that indicates the name of the Function (for
example, Function InterestDue). The name does need to follow the
standard variable naming conventions.
arglist An optional list of variables representing arguments that are passed
to the Function procedure when it is called. Multiple variables are
separated by commas. Chapter 5
type An optional statement that specifies the data type of the result
returned by the Function procedure. For example, a function returning
an integer value would have As Integer in this space.
statements An optional group of statements to be executed within the Function
procedure.
99
Part 2: Visual Basic for Applications