VBA: Setting a Global Variable to a Value

screech

Active Member
Joined
May 27, 2004
Messages
296
I have a global variable called "Start" which I set equal to the Timer so I can subtract that from the current time to record the elapsed time that a macro is running. I set this by using:
Code:
Option Explicit
Public Start As Single

At the beginning of a sub, I define this global variable as Start = Timer. The sub goes to call another sub where the Start variable is also used. When it gets to the code within this second sub, it gives me an error called "ambiguous name detected: Start". What am I doing wrong? Am I not defining the global variable correctly? Is there a way to define the variable as a specific value globally? Do I have to put an identical Option Explicit code at the top of each module in which all subs are stored?

I appreciate the insight into this whole global variable problem.

EDIT: All subs called are actually defined as "Functions", if that matters.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Have you declared Start more than once?
 
Upvote 0
And it isn't declared in anywhere else? In another module for example.
 
Upvote 0
Yes, it's in another module, but would Excel be looking at all modules for variables even if they are not being executed at the time? I was thinking that the public declaration only applies to the code being executed in the module at the time.
 
Upvote 0
By declaring iy as Public it is available to all modules.

So if you have it declared in more than 1 module you have an ambiguous name.
 
Upvote 0
I didn't believe that Excel worked like that. Now I understand. A variable name adjustment should fix it. Thanks for all your help and info, Norie!
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,254
Members
448,556
Latest member
peterhess2002

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top