Global Vs. Public Variable.

Evagrius Ponticus

Well-known Member
Joined
May 24, 2007
Messages
1,467
Hello,

Can anyone please explain the difference between declaring a variable as global or as public. Aren't they both available to the entire project including forms? Thank you.
 
Yeah, with that declaration statement in a code module, if you select 'Global' and press F1 it even opens up the "Public Statement" help topic in the VBA helpfile. As far as I can see they both do exactly the same thing scope-wise in VBA, but Global can't be used in class modules.

Some Excel developers do use Global. If I recall correctly, I *think* xld said on another thread in here that he uses Global because he finds that the word 'Global' is a better descriptor.
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Could I jump into this discussion ? I'm trying to do something similar inasmuch as I would like to define a variable in one place (if possible) so that it has the same value everywhere.
In my naivety (?), I defined the following in Thisworkbook
Code:
Option Base 1
Option Explicit
Public tech_sheet As String
Private Sub Workbook_Activate()
tech_sheet = "Technical Sheet"     ' Define the name of the technical sheet
End Sub
Now, I run a userform that contains the following code
Code:
Private Sub userform_Initialize()
code ....
call fill_room_combo
end sub
Private Sub fill_room_combo()

Dim rOldList As Range
Dim number_of_rows As Long

' Remove any duplicates in the country column (A) on sheet 2
' and sort the results. Re-populate the country combo after that
Call ThisWorkbook.SortAndRemoveDupes("J")

'Set range variable to list we want
Set rOldList = Sheets(tech_sheet).Range("J1", Sheets(tech_sheet).Range("J65536").End(xlUp))
and it falls over on the reference to tech_sheet.

Do I really have to duplicate the definition for tech_sheet as public in any module/userform that might use it ?
 
Upvote 0
If you put the declaration
Public tech_sheet As String
in a Normal module (outside of and before any procedures), tech_sheet will be available to any sub in the workbook, with the value set in the Workbook_Activate routine.
 
Upvote 0
If I have code that opens another workbook and then executes an application.run statement to show a userform in that other book, can I pre-fill some of the fields within that userform with variables that have been declared as "public" in the first book (the one with the code that executes opening another workbook's userform)?
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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