Run-Time error 1004 - Method Ontime of object application failed - Please assist

Anton1999

New Member
Joined
Aug 5, 2020
Messages
25
Office Version
  1. 2013
Platform
  1. Windows
Hi Everyone, could someone please assist me.

Following code:
VBA Code:
Option Explicit
Public sTimer As Boolean
Public sTimer2 As Boolean
Public sTimer3 As Boolean
Public sTimer4 As Boolean
Public sTimer5 As Boolean
Public sTimer6 As Boolean
Public sTimer7 As Boolean
Public sTimer8 As Boolean
Public sTimer9 As Boolean
Public sTimer10 As Boolean
Public wsSheet1 As Worksheet, wsTimeSummary As Worksheet


Sub Start_Timer()
        Application.OnTime Now + TimeValue("00:00:01"), "IncreamentTimer"
End Sub

Sub IncreamentTimer()
        If ThisWorkbook.Sheets("Sheet1").Range("K9").Value = "" Then ThisWorkbook.Sheets("Sheet1").Range("K9").Value = TimeValue(Now)
        If ThisWorkbook.Sheets("Sheet1").Range("K8").Value = "" Then ThisWorkbook.Sheets("Sheet1").Range("K8").Value = TimeValue(Now)
        ThisWorkbook.Sheets("Sheet1").Range("K8").Value = ThisWorkbook.Sheets("Sheet1").Range("K8").Value + TimeValue("00:00:01")
        If ThisWorkbook.Sheets("Sheet1").Range("J8").Value = "" Then ThisWorkbook.Sheets("Sheet1").Range("J8").Value = "Timer ON"
        ThisWorkbook.Sheets("Sheet1").Range("J9").Value = "Timer Start Time"
        ThisWorkbook.Sheets("Sheet1").Range("J8").Font.Color = vbGreen
        sTimer = True
        Start_Timer
End Sub

Sub PauseResume_Timer()
        If ThisWorkbook.Sheets("Sheet1").Range("K9").Value <> "" Then
                If sTimer = True Then
                        sTimer = False
                        Application.OnTime Now + TimeValue("00:00:01"), "IncreamentTimer", schedule:=False
                        ThisWorkbook.Sheets("Sheet1").Range("J8").Value = "Timer Paused"
                        ThisWorkbook.Sheets("Sheet1").Range("J8").Font.Color = vbRed
                Else
                        Application.OnTime Now + TimeValue("00:00:01"), "IncreamentTimer"
                        ThisWorkbook.Sheets("Sheet1").Range("J8").Value = "Timer Resumed"
                        ThisWorkbook.Sheets("Sheet1").Range("J8").Font.Color = vbGreen
                        
                End If
        Else
                MsgBox "You can only click Pause/Resume button when Timer is On.", vbExclamation, "Timer Off!"
        End If
End Sub

Sub Stop_Timer()
    
    With ThisWorkbook
        Set wsSheet1 = .Worksheets("Sheet1")
        Set wsTimeSummary = .Worksheets("Time Summary")
    End With
    
    On Error Resume Next
    Application.OnTime Now + TimeValue("00:00:01"), "IncreamentTimer", schedule:=False
    
    If Err = 0 Then
    
        With wsSheet1
            .Range("L9").Value = Format(.Range("K8").Value - .Range("K9").Value, "hh:mm:ss")
            .Range("J8").Value = "Timer OFF"
            .Range("J8").Font.Color = vbBlack
        End With
        
        With wsTimeSummary
            .Range("D12").Copy
            .Range("E12").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
                                       SkipBlanks:=False, Transpose:=False
        End With
        Application.CutCopyMode = False
    Else
        
        MsgBox "Timer Is currently OFF.", vbExclamation, "Timer Is OFF!"
        
    End If
    
End Sub


Sub Reset_Timer()
        ThisWorkbook.Sheets("Sheet1").Range("K8").Value = ""
        ThisWorkbook.Sheets("Sheet1").Range("K9").Value = ""
        ThisWorkbook.Sheets("Sheet1").Range("J8").Value = ""
        ThisWorkbook.Sheets("Sheet1").Range("J9").Value = ""
        ThisWorkbook.Sheets("Sheet1").Range("L9").Value = ""
End Sub

What I have noticed is when you press pause JUST before the timer wants to add another second. It crashes and gives the error in the topic.
How can I fix this? Everything else works well. It just happens now and then.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,012
Messages
5,834,920
Members
430,326
Latest member
tomwax46

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