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
 
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.[

PLEASE post the CM method! I really want to see how that would work!

Thanks

:)
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Create a class and call it cExample. Paste the following code:

Code:
Option Explicit
Private c_MyArray As Variant

Property Get MyArray() As Variant
    MyArray = c_MyArray
End Property

Property Let MyArray(p1 As Variant)
    c_MyArray = p1
End Property

Public Sub DebugShowArray()

Dim l As Long

For l = LBound(c_MyArray) To UBound(c_MyArray)
    
    Debug.Print c_MyArray(l)

Next l

End Sub

Then create a new module and paste this code:

Code:
Option Explicit

Sub modMain()

Dim oExample As cExample

Set oExample = New cExample

oExample.MyArray = Array(1, 2, 3, 4, 5)

oExample.DebugShowArray

DoubleArray oExample

oExample.DebugShowArray

End Sub

Function DoubleArray(ByRef p As cExample)

Dim l As Long
Dim tmpArray As Variant

tmpArray = p.MyArray

For l = LBound(tmpArray) To UBound(tmpArray)
    
    tmpArray(l) = tmpArray(l) * 2

Next l

p.MyArray = tmpArray

End Function


Then run modMain.

It's a pretty poor example, but hopefully highlights the principle.
 
Upvote 0
Create a class and call it cExample. Paste the following code:

Code:
Option Explicit
Private c_MyArray As Variant

Property Get MyArray() As Variant
    MyArray = c_MyArray
End Property

Property Let MyArray(p1 As Variant)
    c_MyArray = p1
End Property

Public Sub DebugShowArray()

Dim l As Long

For l = LBound(c_MyArray) To UBound(c_MyArray)
    
    Debug.Print c_MyArray(l)

Next l

End Sub

Then create a new module and paste this code:

Code:
Option Explicit

Sub modMain()

Dim oExample As cExample

Set oExample = New cExample

oExample.MyArray = Array(1, 2, 3, 4, 5)

oExample.DebugShowArray

DoubleArray oExample

oExample.DebugShowArray

End Sub

Function DoubleArray(ByRef p As cExample)

Dim l As Long
Dim tmpArray As Variant

tmpArray = p.MyArray

For l = LBound(tmpArray) To UBound(tmpArray)
    
    tmpArray(l) = tmpArray(l) * 2

Next l

p.MyArray = tmpArray

End Function


Then run modMain.

It's a pretty poor example, but hopefully highlights the principle.

Thanks for the CM example.

Looking at that and your previous method of passing the array, would you say using CMs this way is an overkill? If not, what benefits are there for using the CM method as opposed to the passing method?
 
Upvote 0
It really really really depends on what you are trying to achieve.

For example, you could put a function in cExample that fills the data into the Array from a designated source.
You could then make it a read-only property by removing the 'let/set' part of the class.
Then you could make the class object itself public, and not be worried that the data will ever change - because it can't, we can only 'get' the property!


Code:
public cExample as oExample

sub modMain()

set cExample = new oExample

oExample.FillArray

end sub

sub AnotherSub()

something = oExample.MyArray 'will work as we have a get property
oExample.MyArray = Array(1,2,3,4,5) 'will not work as we have not got a let/set property


end sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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