Application.OnTime won't stop

kopite2002

New Member
Joined
Aug 17, 2010
Messages
5
Evening All

I've trawled through many existing posts on this subject, but unfortunately still cannot get my code to stop running. Issue exists where a file (completely unrelated) is already open, and then I open my file that has the Application.OnTime code.

The following code is in the 'ThisWorkbook' Object:
VBA Code:
Dim SaveTime As Date

Private Sub Workbook_Open()
    SaveTime = Now + TimeValue("01:00:00")
    Application.WindowState = xlMinimized
    Application.OnTime SaveTime, "SaveMe", True
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim xRet As Boolean
        
    xRet = IsDBOpenLocally
    If xRet Then
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        Application.StatusBar = "**** Application is closing. Please wait..."
        If Not DBwb.ReadOnly Then
            Windows(DBFileName).Visible = True
            DBwb.Save
        End If
        CloseDB
        Application.StatusBar = False
        Application.ScreenUpdating = True
    End If
    On Error Resume Next
    Application.OnTime SaveTime, "SaveMe", False
End Sub

And this is the "SaveMe" sub in the Modules:
VBA Code:
Sub SaveMe()
    SaveTime = Now + TimeValue("01:00:00")
    Application.DisplayAlerts = False
    ThisWorkbook.Save
    Application.DisplayAlerts = True
    Application.OnTime SaveTime, "SaveMe", True
End Sub

After closing the worksheet with the above code in, but keeping the original file open (so the excel instance is still running), the code will still run as scheduled, therefore re-opening the file. What have I done wrong?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,917
Office Version
  1. 365
Platform
  1. Windows
Try declaring the variable SaveTime as Public, and declaring it in your regular module, not ThisWorkbook . . .

VBA Code:
Public SaveTime As Date

Hope this helps!
 
Solution

kopite2002

New Member
Joined
Aug 17, 2010
Messages
5
Try declaring the variable SaveTime as Public, and declaring it in your regular module, not ThisWorkbook . . .

VBA Code:
Public SaveTime As Date

Hope this helps!
Thanks Domenic, that has indeed worked. I knew it would be something simple, but been looking at it for too long to see if for myself!
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,917
Office Version
  1. 365
Platform
  1. Windows
You're very welcome, glad I could help.

Cheers!
 
Learn Excel from Bill Jelen

Understanding data is crucial, and the easiest place to start is with Microsoft Excel.

Forum statistics

Threads
1,151,697
Messages
5,765,987
Members
425,320
Latest member
Galin

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