Understanding scope

medic5678

Board Regular
Joined
Nov 13, 2018
Messages
67
In order to have a variable visible to all controls in a particular form, does it need to be global? Is there a way to declare a variable to have the scope of all procedures triggered by controls in a from, or would you need to put all the code for the subroutines in a module, with a module scoped varaible?

assuming userform1 with 10 controls, all manipulating the same array.

When the form in initiated, can you declare the array there, in such a way that all the controls would have access to the array, but the array wouldn't have scope to anything outside the form?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Declare the array (as private - this is optional) at the top of the UserForm Module and then Populate it in the initialize event
 
Upvote 0
so the declaration would be

private myarray as variant

and then in the initialize event

myarray = range(a1:z4). Then any subroutine in the form can see this?

Whats interesting to me is that nowhere (that I can see) does Microsoft suggest this sort of variable scope. They list the 3 types of scope as procedure, module and global. They don't say anything about userform scope. Is a userform is a type of module?

Other than private, what are the other options are far as declaration at the top of a userform - and how would they act differently?

Thanks.
 
Upvote 0
A userform module is actually a Class module with a User interface so if follows the same rules as class modules.

You could use Dim at the top of the userform module which is the same as Private and the variable will be seen throughout the userform module.

If you declare the variable as Public at the Top of the UserForm Module, the variable can be seen througout the VBProject.
 
Upvote 0
Thanks for clearing that up everyone! Now I get it :).

Just a note worth mentionning with regards to calling Public Variables in a UserForm Module from outside the Userform and that's the fact that you will need to fullly qualify the variable with the UserForm name just like when Setting or Retrieving native UserForm Properties.

So if you have this in the UserForm Module :
Code:
Public X As Long

Private Sub UserForm_Initialize()
    X = 1000
End Sub

Then you want to get the value stored in the X variable from outside the userform module, you should fully qualify it like this :
Code:
Sub Test()
    MsgBox UserForm1.X
End Sub
 
Upvote 0
Just a note worth mentionning with regards to calling Public Variables in a UserForm Module from outside the Userform and that's the fact that you will need to fullly qualify the variable with the UserForm name just like when Setting or Retrieving native UserForm Properties.

So if you have this in the UserForm Module :
Code:
Public X As Long

Private Sub UserForm_Initialize()
    X = 1000
End Sub

Then you want to get the value stored in the X variable from outside the userform module, you should fully qualify it like this :
Code:
Sub Test()
    MsgBox UserForm1.X
End Sub

Actually, you just saved me a considerable amount of groping around here, as I'm adding buttons to pop up other userforms to manipulate the array. Should I ever cross you in Starbucks, I owe you a Cafe Mocha Grande :).
 
Upvote 0
Actually, you just saved me a considerable amount of groping around here, as I'm adding buttons to pop up other userforms to manipulate the array. Should I ever cross you in Starbucks, I owe you a Cafe Mocha Grande :).
Glad I could help (y)
 
Upvote 0
The people here have been phenomenal. When you're new to this, you have no clue how to get started. I've tried to take VBA classes, but I just can't suffer through them, as my internet is so lousy (satellite, I live in Timbuktoo) I can't watch the videos without constant stopping/stalling. There's no substitute for doing and to be able to ask a question here and have someone take time to answer it (clueless newbies can be dumber than rocks), is an enormous help. Actually just a few days away from finishing my first VBA application. It's all starting to gel.
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,855
Members
449,096
Latest member
Erald

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