How to display value of global variable?

  • Thread starter Thread starter JoeU2004
  • Start date Start date
J

JoeU2004

I have a global variable in a module, outside any procedure, declared as:

Private cnt as Long

In the Immediate Window, I tried the command:

print cnt

to no avail. If I change Private to Public, the print command works. But I
want it to be Private.

FYI, the variable cnt is incremented by some procedures.
 
PS....
I have a global variable in a module, outside
any procedure, declared as:
Private cnt as Long

Hmm, forgot to ask my question, which is....

How can I display its value while I am in the VBA editor, notably when I am
not at a breakpoint in a procedure that uses the variable?

Hovering the cursor over the global declaration did not work.


----- original message -----
 
HiJoe,

I believe that the reason it des not work is that Private restricts the
variable to the module in which it is declared. The Immediate window becomes
like another module. If you place a sub in the module in which it is declared
then you can print it to the immediate window.

Examples:-

Private cnt As Long

Sub test1()
cnt = 8
End Sub

Sub test2()
Debug.Print cnt
End Sub
 
OssieMac said:
If you place a sub in the module in which it is declared
then you can print it to the immediate window.

Well, duh! Shoulda thought of that myself. Thanks.


----- original message -----
 
Errata....
Well, duh! Shoulda thought of that myself.

Oh yeah, I did eventually. I discovered y'hafta have forethought to do it
that way.

If you add a procedure to the module later, all variables are reinitialized
to zero (!), even Static variables in another procedure in the module.

I guess it's a good idea ;-) to always have a dummy Sub and Function whose
contents you can change.

I'm surprised that hovering the cursor over the declaration does not work.

Aha! I just stumbled on a way to get hovering to work; well, sorta. If I
set a breakpoint at the beginning of any procedure in the module, then
execute it and stop at the breakpoint, hovering over the Private module
variable shows its value.

Unfortunately, Reset'g to abort the stopped procedure also resets all
variables again. Sigh.

Anyway, thanks for your explanation of the root cause of the problem. I
guess there is no way to display Private module variables without
forethought.


----- original message -----
 
at the start of the module put
OPTION PRIVATE MODULE

from HELP:
When a module contains Option Private Module, the public parts, for example,
variables, objects, and user-defined types declared at module level, are
still available within the project containing the module, but they are not
available to other applications or projects.
 
Back
Top