variables module

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Variables in a module

Variables in a module

resolvedResolved · Medium Priority · Version 2007

Edited on Wed 19 Jan 2011, 10:05

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

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Hiding Formulae in the Formula Bar

It is possible to protect the contents of a cell reference(s) from amendment by applying cell protection. The contents of the cell reference can also be stopped from displaying in the formula bar.

Step 1: Select Format > Cells > Protection.

Step 2: Tick Hidden option. Ensure Locked is ticked

Step 3: Select Tools > Protection > Protect Sheet

(Ensure "Protect worksheet and contents of locked cells" is ticked)


View all Excel hints and tips


Server loaded in 0.08 secs.