Ben has attended:
Excel VBA Intro Intermediate course
Excel VBA Advanced course
Variables in a module
Hi,
Hopefully just a quick question on defining variables. See the below code.
======
sub FirstSub
for x = 1 to 3
...do some stuff
call SecondSub
next x
End
_____
sub SecondSub
for x = 1 to 3
....do some stuff
next x
End Sub
===============
In this case will VBA get confused as X is used for a second time in the nested sub? What I would wish for is presumably X to be represented by a different variable Y in the second sub. I guess the question is - Is the X defined across the entire module or is it redefined by each sub procedure?
A slight example of the problem is when the first sub defines X as =2, when SecondSub is called will X start at =2 or will it revert back to starting at =1.
Apologies if the description is somewhat confusing.
Thanks
RE: Variables in a module
Hi Ben, thanks for your query. Best practice? Use a separate variable name for clarity. But actually, you'll find a *second* variable is created with the same name and can be called and used. The problems arise when you publically declare a variable in your general declarations at the top of the code and then try to load it up with two different values.
Hope this helps,
Anthony