Stop OnTime event

MrKowz

Well-known Member
Joined
Jun 30, 2008
Messages
6,653
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hey all,

I am helping another user with a procedure that uses an Application.OnTime event to have the code run every minute. However, I need to have a way to stop that event manually. Basically, there would be two buttons, a "Stop" and an "End".

I thought to make a separate procedure that changes the scheduling to false, but I get the error "Method 'OnTime' of object '_Application' failed.

Here are the two codes - any ideas?

Macro to run at start:
Code:
Public Sub GolferTeeTime()
Dim x           As Variant, _
    temptime    As String, _
    rng         As Range, _
    rng1        As String, _
    counter     As Long, _
    LR          As Long
 
counter = 2
Range("D1").Value = Format(Now, "h:mm am/pm")
temptime = Format(Now - TimeValue("00:02:00"), "h:mmam/pm")
x = Application.Match(temptime, Range("A:A"), 0)
If Not IsError(x) Then
    Range("D2:D10").ClearContents
    With Range("A:A")
        Set rng = .Find(temptime, LookIn:=xlValues)
        If Not rng Is Nothing Then
            rng1 = rng.Address
            Do
                Range("D" & counter).Value = rng.Offset(0, 1).Value
                counter = counter + 1
                Set rng = .FindNext(rng)
            Loop While Not rng Is Nothing And rng.Address <> rng1
        End If
    End With
End If
Application.OnTime Now + TimeValue("00:01:00"), "GolferTeeTime", , True
End Sub

Macro to Stop the above OnTime event (this one errors):
Code:
Public Sub StopGolferTeeTime()
    [COLOR=red][B]Application.OnTime Now, "GolferTeeTime", , False[/B][/COLOR]
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi. Try like this

Rich (BB code):
Public dTime As Date

Public Sub GolferTeeTime()
Dim x           As Variant, _
    temptime    As String, _
    rng         As Range, _
    rng1        As String, _
    counter     As Long, _
    LR          As Long
 
counter = 2
Range("D1").Value = Format(Now, "h:mm am/pm")
temptime = Format(Now - TimeValue("00:02:00"), "h:mmam/pm")
x = Application.Match(temptime, Range("A:A"), 0)
If Not IsError(x) Then
    Range("D2:D10").ClearContents
    With Range("A:A")
        Set rng = .Find(temptime, LookIn:=xlValues)
        If Not rng Is Nothing Then
            rng1 = rng.Address
            Do
                Range("D" & counter).Value = rng.Offset(0, 1).Value
                counter = counter + 1
                Set rng = .FindNext(rng)
            Loop While Not rng Is Nothing And rng.Address <> rng1
        End If
    End With
End If
dTime = Now + TimeValue("00:01:00")
Application.OnTime dTime, "GolferTeeTime", , True
End Sub

Public Sub StopGolferTeeTime()
    Application.OnTime dTime, "GolferTeeTime", , False
End Sub
 
Upvote 0
Thanks much, VoG. That makes perfect sense once it is put like that.
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,270
Members
452,902
Latest member
Knuddeluff

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