Why are some variables public and others not?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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.
 
Upvote 0
Solution
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.
Yep, that's the problem. For some incomprehensible reason, VBA considers a variable that is added to the watch list from a routine a different instance of that variable even though it's declared in the prolog before any code and is the same variable when the code runs. (sigh) Just one of the many joys of m$ft products.

Does it matter for the purposes of this code whether I declare the variables in the prolog using Dim, Public, Private, or Global?

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,926
Messages
6,175,429
Members
452,641
Latest member
Arcaila

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
Back
Top