I prefer to limit the use of global variables because it makes the code harder to debug and maintain (you don't know where the value is being set). As a result, I pass the necessary variables to my subs and functions--like Kevin suggested.
If you stick with the global variables, then presumably you have a sub that is initializing them. Why not just run this sub each time you launch a macro from the worksheet?
Alternatively, you could run a de-initialization sub as the last step before returning to the worksheet. This sub would restore your global variables to their "blank" state:
Global a As Long
Global x As Date
Global wbkDipline As Workbook
Global wksDipline As Worksheet
Global varHeaders As Variant
Global table_range As Range
Sub Deinitialize()
'Variables lose their values
Set table_range = Nothing
a = 0
x = 0
Set wbkDipline = Nothing
Set wksDipline = Nothing
Set varHeaders = Nothing
End Sub
Sub Initializer()
'Variables gain persistent values
Set table_range = [A1:B10]
a = 3
x = Date
Set wbkDipline = ActiveWorkbook
Set wksDipline = ActiveSheet
varHeaders = table_range.Value
End Sub
Main Topics
Browse All Topics





by: zorvekPosted on 2007-09-27 at 10:36:18ID: 19973126
The easiest way to do this is to not make them global variables. Define them in the routine that uses them and pass them from routine to routine as needed.
Kevin