Scope of variables

Bengt

Active Member
Joined
Mar 4, 2008
Messages
267
I have noticed that if I declare a variable as Public at the top of a module, variables in a user form don't seem to recognize this variable or its value, whereas code in other modules know about the variabel. Is this observation correct? Is there a way to make user form code aware of variables declared as Public in a module?

Bengt
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Works for me.

Module code:
Code:
Option Explicit
Public GlobVar As String
Sub AssignGlobVar()
GlobVar = "Yippee"
End Sub

Userform code:
Code:
Option Explicit
Private Sub UserForm_Click()
MsgBox GlobVar
End Sub
 
Upvote 0
Works for me.

Module code:
Code:
Option Explicit
Public GlobVar As String
Sub AssignGlobVar()
GlobVar = "Yippee"
End Sub

Userform code:
Code:
Option Explicit
Private Sub UserForm_Click()
MsgBox GlobVar
End Sub

Thank you. I didn't get it to work however. Maybe I have made some mistake along the line. If I intepret your answer correctly, there shouldn't be a problem with the scope of public variables whether the declaration is in a module or in the code for a user form? Does it work the other way round too, that if you declare a variable as public in the code for a user form, the variable is also recognized in a code module?

Another question on the same subject: If you have a number of modules in your project, and you have you have public variables in all modules, are all modules aware of each others variables, or do you have to have them in the same module? Is it possible to have a module that doesn't contain anything other than all my publicly declared variables?
Bengt
 
Upvote 0
Well, variable scope is a big subject and you'll probably be best served reading some reference material.

Basically, you can declare a variable as publically accessible, but if this was within the scope of a userform however, it would be available for as long as the userform was in memory. Userform.show followed by Useform.hide would keep it in memory, but Unload Userform would dispose of it and any publicall accessible variables therein.

A module is a bit different within Excel and this is where all of your variables should be put that you want to be accessible by all.

I think however that you're probably over-engineering the solution to whatever problem you may have.

I programme Excel daily for a living, and I very very rarely have cause to use a publically accessible variable.

Much better to centralis(z)e functions that take data in the form of parameters, and return a variable to your calling procedure. Or, make use of Class Modules and basic-inheritance to better control the scope of data and variables.
 
Upvote 0
Well, variable scope is a big subject and you'll probably be best served reading some reference material.

Basically, you can declare a variable as publically accessible, but if this was within the scope of a userform however, it would be available for as long as the userform was in memory. Userform.show followed by Useform.hide would keep it in memory, but Unload Userform would dispose of it and any publicall accessible variables therein.

A module is a bit different within Excel and this is where all of your variables should be put that you want to be accessible by all.

I think however that you're probably over-engineering the solution to whatever problem you may have.

I programme Excel daily for a living, and I very very rarely have cause to use a publically accessible variable.

Much better to centralis(z)e functions that take data in the form of parameters, and return a variable to your calling procedure. Or, make use of Class Modules and basic-inheritance to better control the scope of data and variables.

Thanks for your time. Maybe you are right about the over-engineering thing. Can I just pester you with a few questions to see that I have got it right:
1) Is it ok - Even recommendable? - to store all your public variables in one module, and just have them there, without any code or anything. In that case, will the variables be accessible for all code in the project, code in modules and code in user forms?
2) If I have a public variable declared in a user form, that variable is available to the project as long as the userform stays in memory, but if I unload the form then the declaration goes away too?

Thanks again

Bengt
 
Upvote 0
(1) It is ok, and yes they would be available.
(2) That is correct, but if you follow (1), then you won't need to declare one in a userform
 
Upvote 0
A variable declared in a userform module is only available in the userform module, no matter if it's been declared as Public.
 
Upvote 0
A variable declared in a userform module is only available in the userform module, no matter if it's been declared as Public.

That seems a bit contradictory to the answer I just got from nuked. Which one is correct?

Bengt
 
Upvote 0
Bengt

Well I've definitely made the mistake of thinking that declaring a variable as Public in a class module, like a userform module, made it available elsewhere.
 
Upvote 0
Surely if the userform or class is instantiated then you can access it's public members, which I think is what I've said above?
 
Upvote 0

Forum statistics

Threads
1,213,487
Messages
6,113,938
Members
448,534
Latest member
benefuexx

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