static variable

K

Ken

Hi,

does vba provide a static variable to keep track of how
many times the specific macros been used?

thanks!

ken
 
G

Guest

Hi Jonathan,

What do you mean "code"? Do I have to hardcode it? Or,
Can I program it?

Thanks!

Ken
 
J

Jonathan West

Hi Jonathan,

What do you mean "code"? Do I have to hardcode it? Or,
Can I program it?

In each routine, you would have to include a static variable, and update it
each time the routine runs.

If you want the count to continue even between Word sessions, then you would
need to store the count somewhere on disk, maybe in an INI file or int he
registry.
 
G

Guest

Thanks!
so where should i write my static variable? Could I use
Dim.... to get static variable?
 
J

Jonathan West

Thanks!
so where should i write my static variable? Could I use
Dim.... to get static variable?

You use the Static statement instead of the Dim statement. There is a code
example in the VBA help file.
 
M

Malcolm Smith

A static variable has the same scope as a Dim (or Private) variable and
you shove it in a routine.

The difference between a static variable and a normal variable is that the
value of the static variable is NOT destroyed when the routine is left.
However, the scope of the variable is that so it can only be accessible
from the routine itself.

In other words it's half way between a global and a private variable.


Now, I have never placed one at module level before. In fact, I have no
idea if this is allowed; the reason that if I need a static variable at
module level then I ought to be looking at a class rather than a module.

Hope this makes some semblance of sense.

Cheers
- Malc
www.dragondrop.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top