vba variables

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 » VBA Variables

VBA Variables

resolvedResolved · Low Priority · Version 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:

Finding cells that have data restrictions

Click anywhere on the worksheet.
On the Edit menu, click Go To.
Click Special.
Click Data validation.
Click All.

View all Excel hints and tips


Server loaded in 0.08 secs.