Auto Save macro keeps running after workbook is closed

MurrayBarn

New Member
Joined
May 27, 2012
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hi There

I have a short macro in a workbook (not in Personal.xlsb) that saves said workbook every ten minutes as I am using some functions that prevents Excel autosave running for some reason. It works really well but the problem is if I close said workbook, the macro keeps running. It opens the workbook and saves it every ten minutes even if closed. The only way to stop this is close Excel completely and then open up the other workbooks. It is quite annoying as I often have lots of workbooks open. Below is the code I am using. My question is is there some VBA code that will remove the below code from whatever memory Excel keeps when this particular workbook is closed?

VBA Code:
Sub SaveThis()
   
     Application.DisplayAlerts = False
     ThisWorkbook.Save
     Application.DisplayAlerts = True
    
     Application.OnTime Now + TimeValue("00:10:00"), "SaveThis"
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You can fix the problem by cancelling the pending OnTime event when you close the workbook. To do that, the code must provide the exact time that the OnTime procedure is scheduled to run, and therefore we must store the time in a Public variable, which is then used to schedule or cancel the OnTime event. Your code needs a bit of restructuring, along with additional code.

Code in a standard module:
VBA Code:
Option Explicit

Public RunWhen As Double

Sub StartTimer()
    RunWhen = Now + TimeValue("00:10:00")
    Application.OnTime RunWhen, "SaveThis", , True
End Sub

Sub StopTimer()
    On Error Resume Next
    Application.OnTime RunWhen, "SaveThis", , False
End Sub

Sub SaveThis()
     Application.DisplayAlerts = False
     ThisWorkbook.Save
     Application.DisplayAlerts = True
     StartTimer
End Sub
Code in the ThisWorkbook module:
VBA Code:
Option Explicit

Private Sub Workbook_Open()
    StartTimer
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    StopTimer
End Sub
 
Upvote 0
You can fix the problem by cancelling the pending OnTime event when you close the workbook. To do that, the code must provide the exact time that the OnTime procedure is scheduled to run, and therefore we must store the time in a Public variable, which is then used to schedule or cancel the OnTime event. Your code needs a bit of restructuring, along with additional code.

Code in a standard module:
VBA Code:
Option Explicit

Public RunWhen As Double

Sub StartTimer()
    RunWhen = Now + TimeValue("00:10:00")
    Application.OnTime RunWhen, "SaveThis", , True
End Sub

Sub StopTimer()
    On Error Resume Next
    Application.OnTime RunWhen, "SaveThis", , False
End Sub

Sub SaveThis()
     Application.DisplayAlerts = False
     ThisWorkbook.Save
     Application.DisplayAlerts = True
     StartTimer
End Sub
Code in the ThisWorkbook module:
VBA Code:
Option Explicit

Private Sub Workbook_Open()
    StartTimer
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    StopTimer
End Sub
Thank you, I'll give it a go
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,614
Members
449,039
Latest member
Mbone Mathonsi

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