stop application on time

p4nny

Board Regular
Joined
Jan 13, 2015
Messages
246
Hi

My code autosaves a workbook every 10mins. Problem is, the workbook keeps opening once it is closed.

Here is my code:

Workbook
Application.OnTime Now + TimeValue("00:10:00"), "savethis"

Module1
Sub savethis()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.OnTime Now + TimeValue("00:10:00"), "savethis"
End sub

Please could you help me cancel this on or before event workbook close.

I'm new to vba.

Cheers
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
where you got the code from should have had a stop event availabe

what you need to do is something like
Sub StopTimer()
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=False
End Sub

i.e cancel the schedule
 
Upvote 0
My code autosaves a workbook every 10mins. Problem is, the workbook keeps opening once it is closed.

You've done very well for someone who is "new to vba".

Make the following changes, indicated in red.

Module1
Rich (BB code):
Public saveTime As Double    ' I avoid type Date

Sub savethis()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
startSaveTimer
End Sub

Sub startSaveTimer()
saveTime = Now + TimeValue("00:10:00")    ' I prefer TimeSerial(0,10,0)
Application.OnTime saveTime, "savethis"
End sub

Sub stopSaveTimer()
On Error Resume Next
Application.OnTime saveTime, "savethis",, False
End Sub
Workbook
Rich (BB code):
Private Sub Workbook_Open()
startSaveTimer
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
stopSaveTimer
End Sub

Caveat: saveTime is reset to zero when VBA is "reset". This can occur when you click on Run / Reset. In that case, be sure to execute stopSaveTimer manually before clicking on Reset. However, there are other actions that cause VBA to reset global variables automatically and silently; for example, adding a global variable in Module1. AFAIK, the only reliable design is to write saveTime to a worksheet and to read saveTime from it. Usually, that is more trouble than it's worth. It's up to you.
 
Upvote 0
Hi, I have a similar problem, mabe you can help me too? :)

I was trying to adapt your advice into my code but it didn't work out (workbook keeps reopening).

Would be very grateful for help.

Code:
[/COLOR][COLOR=#333333]Sub Update_every_30_sec()[/COLOR]
Sub Update_every_30_sec ()
    Application.Calculation = xlAutomatic
    Application.Calculation = xlManual
    ActiveWorkbook.RefreshAll
    Application.OnTime Now + TimeValue("00:00:10"), "Update_every_30_sec"
End Sub


Sub Auto_Open()
    Application.Calculation = xlAutomatic
    Application.Calculation = xlManual
    ActiveWorkbook.RefreshAll
    Application.OnTime Now + TimeValue("00:00:10"), "Update_every_30_sec"
End Sub


Sub Workbook_BeforeClose(Cancel As Boolean)
    StopMacro = True
    Application.OnTime SchTime, "Update_every_30_sec", , False
    Application.OnTime SchTime, "Auto_open", , False 
End Sub[COLOR=#333333]End Sub[/COLOR][COLOR=#333333]
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,608
Members
449,038
Latest member
apwr

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