OnTime function bugs

mtsleung

New Member
Joined
Sep 19, 2006
Messages
4
Hi guys,

I have written a little macro to repeat a task within a certain time in Excel. If I open 2 Excel files in the same time, then so error would happen. Even I close the Excel file with macro, there is still a message asking me whether Enable or Disable the marco...

So I think that must be related to the OnTime function. Can anyone help??

Thanks

Below is my code:

==============
Sub Refresher()

nextrun = Now + TimeValue("00:00:45")
Application.OnTime nextrun, "refresher"
Call URL_Post_Query

End Sub

Sub URL_Post_Query()
...
...
End Sub
==============
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hi, mtsleung,

before you close the file you need to cancel the procedure

here some basic code to adapt to your suits
workbook module
Code:
Option Explicit

Private Sub Workbook_Open()
sec = 45
Run "do_something"
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Run "stop_it"
End Sub

normal module
Code:
Option Explicit

Public sec As Integer
Public when As Variant

Sub do_something()
when = Now + sec / 60 / 60 / 24
Application.OnTime when, "do_something"
'samplecode writing to sheet
Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = Time
End Sub

Private Sub stop_it()
Application.OnTime EarliestTime:=when, Procedure:="do_something", schedule:=False
End Sub

when other workbooks are open: do you want to
cancel the procedure until the workbook is active again ?
then add this to the workbookmodule
Code:
Private Sub Workbook_WindowActivate(ByVal Wn As Window)
Run "do_something"
End Sub

Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
Run "stop_it"
End Sub
run it at the scheduled time
then add workbook references to your procedure
this line would bug
Code:
Worksheets("thisone").Range("A1") = 1
this would work
Code:
ThisWorkbook.Worksheets("thisone").Range("A1") = 1

kind regards,
Erik
 

Forum statistics

Threads
1,136,925
Messages
5,678,601
Members
419,774
Latest member
MooseWinooski

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