The Importance of Explicit Variable Declaration

We have said that using the Variant data type generally wastes memory and often results in poorer performance. There is an additional, even more important reason to declare all variables explicitly. This has to do with making typing errors, which we all do from time to time. In particular, if we accidentally misspell a variable name, VBA will think we mean to create a new variable!

To illustrate how dangerous this can be, consider the NewBook procedure in Example 5-1, whose purpose is to take the first open workbook, change its contents, ask the user for a name under which to save the changed workbook, and then save the workbook under the new name.

0 0

Post a comment