Starting and stopping code

Mitch_Morgan

New Member
Joined
Dec 6, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I am new to VBA coding and I have read all the threads that seem to cover this but I am doing something wrong. I have built a time loop simulation that I would like to start with a command button and stop. The simulation code all resides in one module "Main". To create the loop I have the following

VBA Code:
Sub Timer()
    Dim Time_loop
    Time_loop = Now() + TimeValue("00:00:01")
    Application.OnTime Time_loop, "Main"


End Sub

The last line in the last Sub I have Timer to cause it to loop. This works fine. I have inserted a form command button and assigned the macro "Timer".

The stop code is

Code:
Public Sub StopTimer()
    Application.OnTime Time_loop, "Timer", schedule:=False
End Sub

I receive a Run-time error '1004' Method 'OnTime of object'_Application Failed

I have read this same error message occuring in other threads but the fixes I have tried do not work
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Rich (BB code):
Public Sub StopTimer()
    Application.OnTime Time_loop, "Timer", schedule:=False
End Sub
The variable Time_loop in this code is an uninitialized variable. It has no value, so it throws an error when you attempt to use it in the statement. You can probably omit it from the statement altogether.
 
Upvote 0
I removed Time_loop and replaced with Now() + TimeValue("00:00:01") in both Subs but stil have the same error
 
Upvote 0
I removed Time_loop and replaced with Now() + TimeValue("00:00:01") in both Subs but stil have the same error
You need it in the first sub, you don't need it in the second sub.
Try the second sub like this:

Public Sub StopTimer()
Application.OnTime "Timer", schedule:=False
End Sub
 
Upvote 0
Getting closer but this time I got a Compile error: Argument not optional. When I hit the Debug it points tp Public Sub StopTimer()
 
Upvote 0
Well, it has been quite a while shince I used OnTime, so I did a look back and it seems you do need the variable in both codes. Try it like this.

VBA Code:
Sub Timer()
    Dim Time_loop
    Time_loop = Now() + TimeValue("00:00:01")
    Application.OnTime Time_loop, "Main"
End Sub

Public Sub StopTimer()  
    Dim Time_loop
    Time_loop = Now() + TimeValue("00:00:01")
    Application.OnTime Time_loop, "Timer", schedule:=False
End Sub
 
Upvote 0
IS there something better than using OnTime, I used that code and I went back to the original error message
 
Upvote 0
IS there something better than using OnTime, I used that code and I went back to the original error message
Actually, this works very well when you get et set up right. Like I said, it has been a while since I last used it, and I have not really tested any of this code. Just doing what I think might work. I will take a closer look and see if I can bet it to work in a test set up. If I do, I will post back to this thread.
 
Upvote 0
Actually, this works very well when you get et set up right. Like I said, it has been a while since I last used it, and I have not really tested any of this code. Just doing what I think might work. I will take a closer look and see if I can bet it to work in a test set up. If I do, I will post back to this thread.
Thanks so much, this has been driving me crazy for about six days
 
Upvote 0
I tried it like this and still could not get it to work without throwing the error. I have checked and rechecked the syntax and it is not the syntax that is the problem. It does not like something about the time. I have tried several thinks related to the time and still get the same OnTime object failed message. But the basic code to loop the '"Main" macro works like it should. You can always use the Crrl + Break keys to stop it. Sorry I could not be more help.

VBA Code:
Sub Timer()
    Dim Time_Loop
    Time_Loop = Now + TimeValue("00:00:03")
    Application.OnTime Time_Loop, "Main"   
End Sub

Sub StopTimer()
    Application.OnTime Now + TimeValue("00:00:03"), "Timer", schedule:=False
End Sub

Sub Main()
MsgBox "OK"
Timer
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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