Public variable

Some videos you may like

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

=ODIN=

Active Member
Joined
Dec 3, 2009
Messages
288
this is not clear. for variables to work between modules, declare them as public at the top of a module outside any 'sub' statement. not certain, but i would declare the class as public if you want to initiate it in other modules.
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,910
That article discusses two ways to write a "property" in a class module.

When I write a custom class, if there is a simple property that's just a container for a value, it can be done one of two ways.

one way declares the "property" as a public variable
Code:
' in class module

Public Tag as String

one way declares it as a Property

Code:
'in a class module

Dim pTag as String

Property Get Tag() As String
    Tag = pTag
End Property

Property Let Tag(inVal as String)
    pTag = inVal
End Property

There really isn't any difference in how the class is used. The syntax for other using that custom class wouldn't change one bit.

Code:
' in a normal module

Sub Test()
    Dim myObj as Class1

    Set myObj = New Class1
    myObj.Tag = "hello"

    MsgBox myObj.Tag
End Sub

That article is not talking about what is usually referred to as a Public variable, i.e. a variable that is available to all routines in all modules.
That article is talking about how to declare a property of a custom class.
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,122,691
Messages
5,597,562
Members
414,156
Latest member
WDMix

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
Top