JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,691
- Office Version
- 365
- Platform
- Windows
I am working on a little UserForm that has three buttons, Win, Lose, and Done. The Win button and Lose button routines have a lot of code in common and share several variables. So I put the common code in a subroutine and put the variable declarations outside the routines so they would be module level. What's odd is that 4 of them behave as if they are module level, but two others, which look to me as if they are declared in the same way, are not.
This code is in a Forms module called uScore.
I put Stops on Sub LoadVars and Private Sub ButtonWin. I then click on the Score button on the sheet which brings up the Score UserForm with the 3 buttons. I then click on the Win button and the code stops on the WinButton sub. The watch list looks like this. Why is the context different? Are they not declared the same way?
Thanks
This code is in a Forms module called uScore.
VBA Code:
'***********************************************************************
' Module Level Variables
'***********************************************************************
Dim BookName As String
Dim SheetName As String
Dim GamesWon As Long, GamesLost As Long
Dim WinOUOld As Long, WinOUNew As Long, LosOUOld As Long, LosOUNew As Long
'************************************************************************
' Load Variables
'
' Loads all variables that are or may be used by more than 1 routine
'************************************************************************
Sub LoadVars()
BookName = ActiveWorkbook.Name
SheetName = ActiveSheet.Name
GamesWon = Range("WinHdr").Offset(1, 0).Value 'Get games won
GamesLost = Range("LossHdr").Offset(1, 0).Value 'Get games lost
End Sub
'***********************************************************
' Win button
'***********************************************************
Private Sub ButtonWin_Click()
Const title As String = "Win Button"
Call LoadVars
Workbooks(BookName).Sheets(SheetName).Activate
. . . rest of code...
I put Stops on Sub LoadVars and Private Sub ButtonWin. I then click on the Score button on the sheet which brings up the Score UserForm with the 3 buttons. I then click on the Win button and the code stops on the WinButton sub. The watch list looks like this. Why is the context different? Are they not declared the same way?
Thanks