Microsoft Office Tutorials and References
In Depth Information
Forcing Variable Declaration
The Variant data type does have its place, for instance when dealing with arrays or conversions of
data types, but you should take care to specify the appropriate data types of all your variables. In so
doing, your macros will run faster, they’ll be easier to read, and they’ll be more reliable.
forcing VAriABlE dEclArATion
Declaring your variables can only be a good thing. It takes a little extra thought and effort, but not
declaring your variables can cause a lot more trouble when reading or debugging your code. Macros
run faster and use less memory when all variables are properly declared.
You can tell if variable declaration is being enforced by seeing if the statement Option Explicit is
at the top of your module. If you do see the Option Explicit statement, write a quick macro that
tries to call an undeclared variable, such as you see depicted in Figure 6-1. When you attempt to
run the macro, you’ll receive a compile error as shown in Figure 6-1, informing you a variable is not
defined. In this scenario, the error occurred because the myName variable was not declared with a
statement such as Dim myName as String .
figurE 6-1
If you do not see the Option Explicit statement at the top of your modules, go into the VBE and
from the menu bar, click Tools Options as shown in Figure 6-2.
Search JabSto ::

Custom Search