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.

Naming Procedures

Every procedure must have a name. The rules governing procedure names are generally the same as for variable names. Ideally, a procedure's name should describe what its contained processes do. A good rule is to use a name that includes a verb and a noun (for example, ProcessDate, PrintReport, Sort_Array, or CheckFilename). Avoid meaningless names such as DoIt, Update, and Fix.

Some programmers use sentence-like names that describe the procedure (for example,

WriteReportToTextFile and Get_Print_Options_ and_Print_Report).

Note 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