To force yourself to declare all the variables that you use, include the following as the first instruction in your VBA module:
This statement causes your program to stop whenever VBA encounters a variable name that has not been declared. VBA issues an error message, and you must declare the variable before you can proceed.
To ensure that the Option Explicit statement is automatically inserted whenever you insert a new VBA module, enable the Require Variable Declaration option in the Editor tab of the VBE Options dialog box. I highly recommend doing so.
This chapter contains many examples of VBA code, usually presented in the form of simple procedures. These examples demonstrate various concepts as simply as possible. Most of these examples do not perform any particularly useful task; in fact, the task can often be performed in a different way. In other words, don't use these examples in your own work. Subsequent chapters provide many more code examples that are useful.
Was this article helpful?