Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick Global variable Question Please! 1

Status
Not open for further replies.

Mightyginger

Programmer
Feb 27, 2003
131
US
I've used Global variables within modules e.g. declare at the top of a module
Global testvariable
and then I can use and abuse that variable within that particular module but I can't seem to be able to declare a global variable within sheet code e.g. in sheet1 (view code) where I have all my code for my comboboxes I would like to pass variables between them using variables.

I get an error if I put at the top of my sheet1 code
Declare anothetestvar
saying that not allowed as public memebers of object variables - whatever that means.

Quite simply is there a way I can define a global variable somewhere and use it within my sheet1 code. Also, while I'm asking, I previously found that if I declared a global variable within, say, Module1 code I couldn't access it from any code written in Module2. Is there a way to pass them between modules please?

Thanks again guys!!!!!




Neil.
 
Hi There,

To pass variables between modules you need to qualify them.
Therefore in the following example:

Global testvar As Integer ' In Module 1
Public Sub macro1()
testvar = 10
macro2
End Sub


Public Sub macro2() ' In module 2
MsgBox Module1.testvar
End Sub

Rgds, John









 
Hi Mightyginger,

A slight correction to what John has posted.

To reference public variables in Class Modules you must qualify them. To reference public variables in normal Modules you should just be able to use the variable name. Class Modules include all the Code Modules attached to various Objects (Sheets, UserForms, etc.).

The reason for the difference is that Classes can be instantiated more than once and, so, there may be more than one copy of the public variable; qualifying them makes the reference unique.

Enjoy,
Tony
 
Okay, thanks John, Skip and Tony!!! Honestly, I'd be lost without you guys!!!

Thanks for all your help and I've got it now.


Neil.
 
I need to re-write a lot of my code!

Cheers Tony! A star for you mate.

Rgds, John

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top