Page 112 - Microsoft Office Excel 2003 Programming Inside Out
P. 112
Microsoft Office Excel 2003 Programming Inside Out
When you read through the procedure, you’ll notice that there is no statement that sends any
value to any outside procedure. As mentioned in Chapter 4, the message box is used only to
send some output to the screen; no value is passed to any other procedures.
For more information on passing values between procedures, see “Passing Arguments to Procedures”
later in this chapter.
With the overview out of the way, it’s time to offer a more formal description of what consti
tutes a Sub procedure.
[Private | Public] [Static] Sub name [(arglist)]
[statements]
[Exit Sub]
[statements]
End Sub
Table 5-1 describes the elements of a Sub procedure.
Table 5-1. Elements of a Sub Procedure
Element Description
Public An optional element that indicates the Sub 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 Sub procedure is accessible only
to other procedures in the module where it is declared.
Static An optional element that indicates the Sub procedure’s local variables are
preserved between calls. The Static attribute doesn’t affect variables that
are declared outside the Sub procedure, even if they are used in the
procedure.
name A required element that indicates the name of the Sub (for example, Sub
AvailableCredit). The name does need to follow the standard variable
naming conventions.
arglist An optional list of variables representing arguments that are passed to
the Sub procedure when it is called. Multiple variables are separated by
commas.
Chapter 5
statements An optional group of statements to be executed within the Sub procedure.
To add a procedure to a code module, follow these steps:
1 Click Tools, Macro, Visual Basic Editor to display the Visual Basic Editor.
2 If necessary, click Insert, Module to create a new code module (or if you want to create
a new module for organizational purposes).
86
Part 2: Visual Basic for Applications