David has attended:
Excel VBA Intro Intermediate course
Excel VBA Advanced course
VBA Variables
I sometime set up the same variable (Dim) in 2 modules which then cause errors when i call procedures between them. When I do this, are there any quick ways to spot which variable is causing the problem?
RE: VBA Variables
Hi David
Thank you for your question
The version of a variable used by a procedure will usually be the one "most local" to the code.
If a variable is used in a procedure, the code first searches in that procedure for a variable of that name, and uses it if it is there. if not then it will search for dim declared variables in the declarations section of the module, and failing that for public variables in any other module. It will always use the first one it finds.
In the example you give, simply look for the variable "most local" to the line of code. It is however, a good idea not to use duplicate variable names for module and procedure level variables
Regards
Stephen