Microsoft Office Tutorials and References
In Depth Information
6.4 Exiting a Procedure
will produce an unacceptable decrease in performance or unless we are very sure that it will not
get changed by accident.
It is important to note that VBA defaults to ByRef if we do not specify otherwise. This means that
the values of arguments are subject to change by the called procedure, unless we explicitly include
the keyword ByVal . Caveat scriptor !
6.4 Exiting a Procedure
VBA provides the Exit Sub and Exit Function statements, should we wish to exit from a
procedure before the procedure would terminate naturally. For instance, if the value of a parameter
is not suitable, we may want to issue a warning to the user and exit, as Example 6-6 shows.
Example 6-6. Using the Exit Sub Statement
Sub DisplayName(sName As String)
If sName = "" then
Msgbox "Please enter a name."
Exit Sub
End If
MsgBox "Name entered is " & sName
End Sub
6.5 Public and Private Procedures
Just as variables and constants have a scope, so do procedures. We can declare a procedure using
the Public or Private keyword, as in:
Public Function AddOne(i As Integer) As Integer
Private Function AddOne(i As Integer) As Integer
The difference is simple: a Private procedure can only be called from within the module in
which it is defined, whereas a Public procedure can be called from within any module in the
Note that if the Public or Private keyword is omitted from a procedure declaration, then the
procedure is considered to be Public .
6.6 Project References
In order for code in one project to call a public procedure in another project, the calling project
must have a reference to the called project.
Search JabSto ::

Custom Search