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

vba variables

ResolvedVersion 2003

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

Tue 26 May 2009: Automatically marked as resolved.

 

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:

Closing all your workbooks Quickly

Hold the SHIFT key down and using the mouse click on the file menu, it will now now CLOSE ALL rather than close. This closes all workbooks down but still leaves the application open.

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.1 secs.