Missing somthing - OnTime?

jim may

Well-known Member
Joined
Jul 4, 2004
Messages
7,486
In my ThisWorkbook:

Code:
Public OpTime As Date

Private Sub Workbook_Open()
OpTime = Now()
Application.OnTime OpTime + TimeValue("0:0:05"), "MyMacro"
End Sub

In Standard Module:
Code:
Sub MyMacro()
MsgBox "You opened this workbook at " & OpTime
End Sub

So why does the Msgbox Show ONLY:
You opened this workbook at

Stepping thru I can see in the Standard Module that OpTime IS EMPTY
But why?

Jim
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
The code being called by the should set the next ontime.
Code:
Sub MyMacro()
    MsgBox "You opened this workbook at " & OpTime
    Application.OnTime Now() + TimeValue("0:0:05"), "MyMacro"
End Sub
 
Upvote 0
The Scope thing has always confused me. Might I conclude... from your statement that ...?

Use of the Public keyword should be limited to Standard Modules, and never ThisWorkbook module. or ????

Always aprreciate your help. Jim
 
Upvote 0
Oops, ignore post.

Thought you wanted the code to run repeatedly.:oops:
 
Upvote 0
The code in the ThisWorkbook module can "see" variables declared in a standard module but not vice versa.
 
Upvote 0
Norie, making the change recommended by Peter makes things work.

Minutes before I saw code similar to yours, but didn't grasp it, as I quickly noticed that a Macro named MyMacro was INSIDE the same Macro named MyMacro. That confused me, so I avoided it. And actually, your code confuses me more... But thanks Jim
 
Upvote 0
The code in the ThisWorkbook module can "see" variables declared in a standard module but not vice versa.


Peter, I LOVE such RULES. They seem to be hidden, thus the need for "prospecting for gold"...

Again, Tks

Jim
 
Upvote 0
Jim

What is it you wanted to do?

Run the code once, 5 seconds after the workbook opened?
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,548
Members
452,927
Latest member
rows and columns

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