Application.OnTime Not Stopping...

burns14hs

Board Regular
Joined
Aug 4, 2014
Messages
76
I have been around the interwebs all day and found multiple posts on multiple forums about this but I cannot get it to work correctly no matter what I've tried. Can someone please tell me why my timer never stops and restarts? I start the timer on workbook open and no matter how many different macros i run that begin with stoptimer and end with starttimer, the workbook always shuts down exactly 15 minutes after original open. What am I doing wrong?

Code:
Public RunWhen As Double

Public Sub StartTimer()
On Error Resume Next
RunWhen = Now + TimeValue("00:15:00") ' hh:mm:ss
Application.OnTime EarliestTime:=RunWhen, Procedure:="CloseDownFile", Schedule:=True

End Sub

Public Sub CloseDownFile()

StopTimer

Dim bCancel As Boolean
Application.run "ThisWorkbook.Workbook_BeforeClose", bCancel

On Error Resume Next
Application.StatusBar = "Inactive File Closed: " & ThisWorkbook.Name
ThisWorkbook.Close SaveChanges:=False
End Sub

Public Sub StopTimer()

On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, Procedure:="CloseDownFile", Schedule:=False

End Sub
 
Mark - I put your code into an empty brand new workbook and it worked perfectly. I then put it into the massive workbook that I am trying to fix and same problem as before. What this means to me is that somewhere in this workbook just after open there is a second instance of StartTimer queuing that is creating a second timer without stopping the first. Then when stop timer is run, it is only running for the latest variable time which is the second one. I appreciate all the help you have provided in me making sure the code is now working correctly so I could troubleshoot the rest. I have now (after a couple hours of searching through probably 200 pages of code) found the culprit and removed it. Thank you so much, again, for your assistance.
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I am glad you got it working and you are most welcome (y)
 
Upvote 0

Forum statistics

Threads
1,216,500
Messages
6,131,016
Members
449,615
Latest member
Nic0la

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