Hi
Hi,
So if i put public x as long in module 1
x in module to will not need to be declared as this will also be long?
Basically yes, I think so , but i guess ZVI wil confim that.
_ .........................
I am mainly popping by with a couple of comments:
_1)
I was ( am ? ) fully in agreement with this:
.......
How to use global variables:
1. Define global variables only in standard module which is created via VBE - Insert - Module.
In the top line of the created Module1 (but below the Option Explicit) type:
Dim MyGlobalVar1
2. Use it in any module like this:
Rich (BB code):
Sub LetMyGlobalVar()
MyGlobalVar1 = Now
End Sub
Sub GetMyGlobalVar()
MsgBox MyGlobalVar1
End Sub
_.........In fact i thought i had done that a lot before.
_ But......Today i was playing around with global variable of this nature and experienced problems:
If i follow the instructions of ZVI ( putting something like
Code:
[color=blue]Option[/color] [color=blue]Explicit[/color]
[color=blue]Dim[/color] MyGlobalVar1
in a normal module ) and then in another normal module the other stuff, it does not work!?! ( This puzzles me particularly as if I change to lower case a few letters like so
__ Myglobalvar1
___ , then VBA changes them back to Upper case as in
__ MyGlobalVar1
___suggesting it has some “memory” of my global variable, (
which incidentally remains if I delete the defining line!!!!!! ( and still remains if i close and open the Workbook !?! ) ) )
_ ...However the codes do not work, erroring that the Variables MyGlobalVar1 is not defined.
I got over the problem by trial and error by changing the code defining the Global Variable to
Code:
[color=blue]Option[/color] [color=blue]Explicit[/color]
[color=blue]Public[/color] MyGlobalVar1
I am not quite sure what the problem is, but if you encounter a similar problem you may want to try the
Public bit instead of
Dim
_..............................
_2)
Be very careful when You are editing / changing global variables. I would recommend you save, close and reopen a Workbook before running any code after changing / editing anything involving Global variables. If you do not do this , I find that Excel crashes frequently ( badly! ) . I expect it can get confused easily as to which variable exist or do not exist or which do or do not have something in them. – Remember that Global variables “live on” after a code finishes. ( _.....There may be occasions when this is wise to do after you have finished using a Global variable...
Code:
[color=blue]Sub[/color] GetMyGlobalVar()
MsgBox MyGlobalVar1
[color=blue]Set[/color] MyGlobalVar1 = [color=blue]Nothing[/color]
[color=blue]End[/color] [color=blue]Sub[/color]
_ .....)
Alan