variables module
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

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

replyReply Wed 19 Jan 2011, 10:04 Edited on Wed 19 Jan 2011, 10:05Delegate Ben said...

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

For upcoming training course dates see: Pricing & availability

replyReply Wed 19 Jan 2011, 10:26Trainer Anthony said...

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

 

Please browse our web site to find out more about
excel computer training and other Microsoft training courses.

Excel tip:

Deleting a range of cells using the autofill handle

Firstly, select the range of cells for which you would like to clear the contents. Then drag the autofill handle to the the top left corner of the selection whilst holding down the shift key. Your selected contents should then be deleted.

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard