Application.OnTime continuously opening my workbook

Afro_Cookie

Board Regular
Joined
Mar 17, 2020
Messages
103
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Sub SaveThis()

Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True

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

End Sub

This is in WB1. If I have a second Workbook open, it will automatically open WB1 each time the Application.Ontime timer runs down. I only want the macro to run if this particular workbook is open.

Any ideas on how to get around this would be really appreciated.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Put this in a standard code module...

VBA Code:
Public NextTime As Date

Sub SaveThis()

Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True

NextTime = Now + TimeValue("00:00:10")
Application.OnTime NextTime, "SaveThis"

End Sub

Sub SaveThisEnd()
    On Error Resume Next
    Application.OnTime NextTime, "SaveThis", Schedule:=False
End Sub

Put this in the ThisWorkbook code module...
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    SaveThisEnd
End Sub
 
Upvote 0
I put the code in as written, but now it won't save at all.

Put this in a standard code module...

VBA Code:
Public NextTime As Date

Sub SaveThis()

Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True

NextTime = Now + TimeValue("00:00:10")
Application.OnTime NextTime, "SaveThis"

End Sub

Sub SaveThisEnd()
    On Error Resume Next
    Application.OnTime NextTime, "SaveThis", Schedule:=False
End Sub

Put this in the ThisWorkbook code module...
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    SaveThisEnd
End Sub
 
Upvote 0
I added this so it would run on open. Code works perfectly. Thanks a lot for your help @AlphaFrog
VBA Code:
Private Sub Workbook_Open()
    SaveThis
End Sub

Put this in a standard code module...

VBA Code:
Public NextTime As Date

Sub SaveThis()

Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True

NextTime = Now + TimeValue("00:00:10")
Application.OnTime NextTime, "SaveThis"

End Sub

Sub SaveThisEnd()
    On Error Resume Next
    Application.OnTime NextTime, "SaveThis", Schedule:=False
End Sub

Put this in the ThisWorkbook code module...
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    SaveThisEnd
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,936
Messages
6,122,340
Members
449,079
Latest member
rocketslinger

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