I know that if we name a variable as Public then the variable could keep its value if he passed End Sub between the subs. But my promlem is that I have a variable that gets its value from commandnbutton in a userform, and I get compile error every time I try to set this variable out of the userform sub. I have a listbox of months and after selecting month you press the commandbutton and the macro starts. I know that the value of the variable will move between subs just if H passed Ens Sub,I need to use this variable value in other subs too and I need it to be dynamic, anyone knows maybe what can help me?
this is the userform sub:
Public MainWB As Workbook
Public VisualWB As Workbook
Public VisualWS As Worksheet
Public VacationWS As Worksheet
Public HealingWS As Worksheet
Public IllnessWS As Worksheet
Public Lists As Worksheet
Public MonthCol As Long
Public MonthName As String
Public MonthBefore As Long
Public ColumnSpace As Long
Public LR As Long
Public LC As Long
Public myExtension As String
Public SecondPath As String
Public Table As Range
Public Names As Range
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.Calculation = xlManual
Call initialize
'Here I have a lot of Code
Unload UserForm1
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
Application.Calculation = xlAutomatic
End Sub
this is the module sub:
Option Explicit
Sub initialize()
'The variables will keep their value on all the subs
MonthName = ListOfMonths.Value
Set MainWB = ThisWorkbook
Set VacationWS = MainWB.Worksheets(1)
Set HealingWS = MainWB.Worksheets(2)
Set IllnessWS = MainWB.Worksheets(3)
Set Lists = MainWB.Worksheets("Lists")
Set VisualWS = MainWB.Worksheets("Visual")
With VisualWS
.Range("L1:W1").Find(MonthName, , xlValues, xlWhole).Activate
End With
MonthCol = ActiveCell.Column
MonthBefore = MonthCol - 1
End Sub