Declaring a Sub procedure

A procedure declared with the Sub keyword must adhere to the following syntax:

[Private | Public][Static] Sub name ([arglist])

[instructions]

[Exit Sub]

[instructions] End Sub

♦ Private: (Optional) Indicates that the procedure is accessible only to other procedures in the same module.

♦ Public: (Optional) Indicates that the procedure is accessible to all other procedures in all other modules in the workbook. If used in a module that contains an Option Private Module statement, the procedure is not available outside the project.

♦ Static: (Optional) Indicates that the procedure's variables are preserved when the procedure ends.

♦ Sub: (Required) The keyword that indicates the beginning of a procedure.

♦ name: (Required) Any valid procedure name.

♦ arglist: (Optional) Represents a list of variables, enclosed in parentheses, that receive arguments passed to the procedure. Use a comma to separate arguments. If the procedure uses no arguments, a set of empty parentheses is required.

♦ instructions: (Optional) Represents valid VBA instructions.

♦ Exit Sub: (Optional) A statement that forces an immediate exit from the procedure prior to its formal completion.

♦ End Sub: (Required) Indicates the end of the procedure.

With a few exceptions, all VBA instructions in a module must be contained within procedures. Exceptions include module-level variable declarations, user-defined data type definitions, and a few other instructions that specify module-level options (for example, Option Explicit).

0 0

Post a comment