View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson[_4_] Jim Thomlinson[_4_] is offline
external usenet poster
 
Posts: 1,119
Default Global variables - where do you place them?

Globals are declared outside of procedures and functions at teh top of the
code window. When they are created you are best off to declare the scope of
them at the same time. Something like

Option Explicit

Public MyProjectLevel as String
Private MyModuleLevel as String

Sub Test()
....

As a rule always try to use the smallest possible scope that you can and
avoid Globals like they were evil. Every time you have a global you create
what can become a debugging nightmare. If during run time a global is not the
value that you anticipate that it should be there is almost know way of
knowing which procedure modified it last. Globals have their place but it is
very limited. If you are using globals to avoid the hassel of passing
variables I would say that you are heading down a very dangerous path.

--
HTH...

Jim Thomlinson


"CRayF" wrote:

OK Im still trying to get a handle on the VBA methodology,

Ive now created some €śglobal€ť subroutines that are not in Module3 and are
called by the worksheet modules€¦

I have more than one Sub() in the module3. Many share the same variables
€śDim srcProgramDataInputWs As Worksheet€ť as an example€¦
Where do I define Global variables that can be used throughout anywhere in
the module€¦ What about a variable that can be used anywhere in any worksheet?
This is what I have in one of my modules:

Sub ReBuildProgramSummary(Optional Confirm As Boolean = True)
'
'------------------------------------------------------------------------
' Re-Build Program Summary Template
'------------------------------------------------------------------------

'------- Main File Names used for this WorkBOOK ---------------
'--*** Use Program Input Worksheet as a Master for changes ***
'--------------------------------------------------------------
Dim srcProgramDataInputWs As Worksheet
Dim srcProgramSummaryTemplateWs As Worksheet
Dim srcProgramSummaryWs As Worksheet
Dim srcBettingTemplateWs As Worksheet
Set srcProgramSummaryTemplateWs = Sheets("@TemplateProgramSummary")
Set srcProgramSummaryWs = Sheets("ProgramSummary")
Set srcBettingTemplateWs = Sheets("@TempleteBetting")
Set srcProgramDataInputWs = Sheets("ProgramDataInput")
'--------------------------------------------------------------
'------- Set Variables to Workbook Names ---------------
'--*** Use Program Input Worksheet as a Master for changes ***
'--------------------------------------------------------------

If ActiveSheet.Name = srcProgramSummaryTemplateWs.Name Then
MsgBox "Can't run from Template"
Range("N3").Select
Exit Sub
End If
...more