Application.OnTime continuously opening my workbook

Afro_Cookie

New Member
Joined
Mar 17, 2020
Messages
36
Office Version
  1. 2013
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.
 

Some videos you may like

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,400
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
 

Afro_Cookie

New Member
Joined
Mar 17, 2020
Messages
36
Office Version
  1. 2013
Platform
  1. Windows
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
 

Afro_Cookie

New Member
Joined
Mar 17, 2020
Messages
36
Office Version
  1. 2013
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,127,784
Messages
5,626,850
Members
416,205
Latest member
rondon

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
Top