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

variables module

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

Variables in a module

ResolvedVersion 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:

Navigate with keyboard without losing your active cell

If you like using your keyboard to scroll through your excel document, but want to keep your active cell the same, use the scroll lock, and then use your arrow keys to navigate around the document.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.11 secs.