Why are some variables public and others not?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
1,447
Office Version
  1. 365
Platform
  1. 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.
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?

1615617112606.png


Thanks
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,091
Office Version
  1. 2016
Platform
  1. Windows
In your Watches you miss the Expression Bookname for the Context Uscore.ButtonWin_Click or for the Context uScore.
Add by the Dim statement Bookname to your watch list.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,685
Messages
5,637,803
Members
416,983
Latest member
LessThanAverageUser

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
Top