Run time error 1004 - Method 'OnTime' of object'_Application' failed.

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi, please can someone help me?

I keep getting the following error:

Run time error 1004 - Method 'OnTime' of object'_Application' failed.

It keeps popping up quite often but from different situations on my file but it doesn't always happen when i repeat a certain situation.

Here is all the workbook code

The offending line is in red.

Code:
Option ExplicitConst Timeout As Long = 10 ' 10 minutes


Private Sub Workbook_Open()


Application.OnTime TimeValue("02:00:00"), "sveWrkBk"  '<-- change time to save here hh:mm:ss


dTime = Time
On Error Resume Next
Application.OnTime dTime + TimeSerial(0, Timeout, 0), "CloseMe"
On Error GoTo 0
End Sub


Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)


On Error Resume Next
[COLOR=#ff0000]Application.OnTime dTime + TimeSerial(0, Timeout, 0), "CloseMe", , False[/COLOR]
dTime = Time
Application.OnTime dTime + TimeSerial(0, Timeout, 0), "CloseMe"
On Error GoTo 0


End Sub

Any help would be much appreciated.

Kind Regards

Dan
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I haven't fully tested it, and you may need to make some changes to suit your needs, but try the following code that needs to be placed in the code module for ThisWorkbook...

Code:
Option Explicit

Const Timeout As Long = 10 ' 10 minutes


Dim dtNextSave As Date
Dim dtNextClose As Date


Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    On Error Resume Next
    Application.OnTime dtNextSave, "sveWrkBk", , False
    Application.OnTime dtNextClose, "CloseMe", , False
    On Error GoTo 0
    
End Sub


Private Sub Workbook_Open()


    dtNextSave = Now() + TimeValue("02:00:00")
    Application.OnTime dtNextSave, "sveWrkBk"
    
    dtNextClose = Now() + TimeSerial(0, Timeout, 0)
    Application.OnTime dtNextClose, "CloseMe"
    
End Sub


Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)


        On Error Resume Next
        Application.OnTime dtNextClose, "CloseMe", , False
        On Error GoTo 0
        
        dtNextClose = Now() + TimeSerial(0, Timeout, 0)
        Application.OnTime dtNextClose, "CloseMe"
        
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

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