Public variable

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

=ODIN=

Active Member
Joined
Dec 3, 2009
Messages
292
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
24,208
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:

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,621
Office Version
  1. 2019
Platform
  1. Windows
Thanks for the clarification.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,878
Messages
5,834,200
Members
430,263
Latest member
abz54

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