scope of public variables - Sheet1 can't see ThisWorkbook

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,881
Office Version
  1. 2010
Platform
  1. Windows
In the Workbook_Open() procedure, I set eDate to the current date. When the workbook is opened, a message box pops up showing the current date. But in the sheet1 beforedboubleclick event procedure, I have the following code


eDate = eDate - 1
msgbox(eDate)

and the message box showed -1.

What happened to the public variable eDate?
===============================
Public eDate As Date

Sub Workbook_Open()
eDate = Date
MsgBox (eDate)

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
If I'm going to use a global variable, I'll put it in a standard code module, not in one of the worksheet or thisworkbook modules.
 
Upvote 0
In the Workbook_Open() procedure, I set eDate to the current date. When the workbook is opened, a message box pops up showing the current date. But in the sheet1 beforedboubleclick event procedure, I have the following code


eDate = eDate - 1
msgbox(eDate)

and the message box showed -1.

What happened to the public variable eDate?
===============================
Public eDate As Date

Sub Workbook_Open()
eDate = Date
MsgBox (eDate)

End Sub

Or you could change the codee you have in the beforedblclick event as follows :

Code:
With ThisWorkbook
    .eDate = .eDate - 1
    MsgBox (.eDate)
End With
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,875
Members
452,949
Latest member
Dupuhini

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