Public variable

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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.
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,214,264
Messages
6,118,560
Members
448,837
Latest member
Josephjet

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