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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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!
 
Upvote 0
Solution
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!
 
Upvote 0
You're very welcome, glad I could help.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,661
Messages
6,120,792
Members
448,994
Latest member
rohitsomani

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