Wednesday, January 05, 2005

Writing proper code in VBA

VBA allows you to be slack and not declare variables. If you don't it still uses them, though there could be potential conflicts.
Today I got caught out. A client has a macro sweeping program as part of their corporate anti virus solution. It forced us to go through the code (thousands of lines) and locate all the variables that hadn't been declared and use a Dim or Public statement to declare them.
The difference.
Dim declares a variable and makes it available to all procedures in that module if positioned at the top of the module or just to one procedure if inside the procedure.
Public declares a variable and makes it available across all modules in the project (workbook).