Storing VBA code

In general, a code window can hold four types of code:

♦ Sub procedures: A procedure is a set of instructions that performs some action.

♦ Function procedures: A function is a set of instructions that returns a single value or an array (similar in concept to a worksheet function such as SUM).

♦ Property procedures: These are special procedures used in class modules.

♦ Declarations: A declaration is information about a variable that you provide to VBA. For example, you can declare the data type for variables you plan to use.

A single VBA module can store any number of Sub procedures, Function procedures, and declarations. How you organize a VBA module is completely up to you. Some people prefer to keep all their VBA code for an application in a single VBA module; others like to split up the code into several different modules.

Although you have lots of flexibility regarding where to store your VBA code, there are some restrictions.Event handler procedures must be located in the Code window for the object that responds to the event. For example, if you write a procedure that executes when the workbook is opened, that procedure must be located in the Code window for the ThisWorkbook object, and the procedure must have a special name. This concept will become clearer when I discuss events (Chapter 19) and UserForms (Part IV).

0 0

Post a comment