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
 
You can't put them in any classes (sheets, userforms and class modules) and have them public (unless you hold an instance of the class in scope and access the variable through that), they need to be in a module. They are, however usually unnecessary and generally considered bad practice since they're too easy to change any random pieces of code, it's usually better to pass variables into subs/functions/properties rather than have them exposed as public.
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Nowhere has anyone suggested anything different, aside from

A variable declared in a userform module is only available in the userform module, no matter if it's been declared as Public.

which is incorrect. It can be accessed if, as you say, we have an instance of the userform in the current scope.
 
Upvote 0
You can't put them in any classes (sheets, userforms and class modules) and have them public (unless you hold an instance of the class in scope and access the variable through that), they need to be in a module. They are, however usually unnecessary and generally considered bad practice since they're too easy to change any random pieces of code, it's usually better to pass variables into subs/functions/properties rather than have them exposed as public.

But if I use myarray() in several modules, how else would I declare myarray() other than public?
 
Upvote 0
I suppose the question should be why do you have several modules :)

But to answer your question is hard without knowing what you are trying to achieve. Instead of saying how else you might declare myarray(), you might find that if you shared your purpose, you'd be advised of a way to avoid having to declare it at all.
 
Upvote 0
It depends what you are doing, you would normally pass the array into the functions that require it as a parameter, you can call functions and subs in other modules without using public variables. If you are only using modules then all your code could be in a single module and there would be no need for a public variable.
 
Upvote 0
I've cut up my code into many modules, for example I have a separate module for formatting tables, one for sortng the data, etc.

Initially I populate myarray() with data (in say module populate) then pass it into the sorting module then .....

So I've publically declared myarray() as it's used in several modules.

How else can I get round this (not that actually want to but have read above that it's not good practice to publically declare)?
 
Upvote 0
Well your code has to start somewhere so declare myArray() as private within that procedure. You can then pass it around and do things with it by using functions that accept an array as a parameter.

Code:
Sub modMain()

Dim myArray As Variant
Dim l As Long

myArray = Array(1, 2, 3, 4, 5)

myArray = DoubleArray(myArray)

For l = LBound(myArray) To UBound(myArray)
    Debug.Print myArray(l)
Next l
 
End Sub

Public Function DoubleArray(pArr As Variant) As Variant

Dim l As Long

For l = LBound(pArr) To UBound(pArr)
    pArr(l) = pArr(l) * 2
Next l

DoubleArray = pArr

End Function

Alternatively, you can wrap the array up in a ClassModule and create an instance of it in your current scope, and pass that around.[
 
Upvote 0
Nuked

Perhaps I should rephrase, they aren't accessible unless you go through the userform, which as far as I can see kind of defeats the point.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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