Microsoft Office Tutorials and References
In Depth Information
is found, the statements following the Case Else are executed. The Case Else keyword is
optional. Even if more than one template matches, only a single block of statements will be
executed — the block that follows the first matching template.
Each template in a Select Case statement can contain one or more of the following elements:
n Any expression. The test expression must match the template exactly.
n Two expressions separated by the To keyword. The test expression must fall within
the range specified by the two expressions. For example, 0To50 .
n The Is keyword followed by a comparison operator and an expression. For example,
Is > 10 .
You can use multiple elements in a template, separating them by commas. This example defines a
template that would match if the test expression evaluated to 0, to any value between 5 and 10, or
to any value greater than 25:
Case 0, 5 To 10, Is > 25
A procedure is a section of VBA code that is independent and has been assigned a name. In fact,
every Outlook macro is a procedure, and it is the procedure names that you see listed in the
Macros dialog box. However, whereas all macros are procedures, not all procedures are macros.
This kind of procedure can be very useful in your VBA programming.
Say you have written a number of macros that manipulate and organize your email, tasks, and so
on. Each macro, as part of its operation, needs to sort a list of items into alphabetical order. Rather
than including the VBA code that performs the sort in each and every macro, you can place the
code in a separate procedure and then call the procedure as needed from each macro.
Subs versus Functions
VBA supports two kinds of procedures, Subs and Functions. They are identical except for the fact
that a Function returns data to the calling program, whereas a Sub does not.
To define a Sub procedure, the basic syntax is
Sub subname argumentlist )
‘ Code is placed here