Stopping Application.ontime macro

Raver

New Member
Joined
Oct 21, 2005
Messages
42
Hi there, I got the following macro that runs a macro every minute from the top of the hour. I have difficulty making it stop running when I close the sheet.

Sub ActivateOnTime()
Dim T, When
T = Now()
When = TimeValue(Hour(T) & ":" & Minute(T) + (1 - (Minute(T) Mod 1)) & ":00") ' this will calculate the next number that is divisible by 1
Application.OnTime When, "MyMacro"
End Sub

Sub MyMacro()

'your code here

ActivateOnTime
End Sub

I get the following error when I put this code in 'ThisWorkbook:

Private Sub Workbook_beforeclose(cancel As Boolean)
Application.OnTime When, , , Schedule:=False
End Sub

Error:
Run-time error '1004:
Method 'Ontime' of object '_Application' failed

What should I put in ThisWorkbook to make the Application.Ontime stop from running every minute?

Thanks
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Raver

New Member
Joined
Oct 21, 2005
Messages
42
I found the solution after some searching.

I changed the code a little in the Sub ActivateOntime

I replaced this line:
Application.OnTime When, "MyMacro"

With:
Application.OnTime earliesttime:=When, Procedure:="Mymacro", Schedule:=True

And in this workbook I changed:
Application.OnTime When, , , Schedule:=False

changed it into:
T = Now()
When = TimeValue(Hour(T) & ":" & Minute(T) + (1 - (Minute(T) Mod 1)) & ":00")
Application.OnTime earliesttime:=When, Procedure:="CopyData", Schedule:=False

The Application.Ontime has to match codes have to match eachother in time that is why I got the error.

Hope this can help others.
 
Upvote 0

Forum statistics

Threads
1,191,381
Messages
5,986,300
Members
440,017
Latest member
vasanrajeswaran

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