Run-Time Error 1004 when Looping Application.OnTime Procedure

cykwokad

New Member
Joined
Jan 17, 2022
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
I wrote a macro AutoPing() that allows to loop itself every 5 minutes. It works between before 23:50. But it gets error when it passes to next day (00:00).
Please help me solve this error. Thank you.

Sub AutoPing()

Dim NextSchedule As String


If TimeSerial(Hour(Now), Application.WorksheetFunction.Floor(Minute(Time), 5) + 5, 0) <> TimeSerial(23, 55, 0) Then

NextSchedule = TimeSerial(Hour(Now), Application.WorksheetFunction.Floor(Minute(Time), 5) + 5, 0)

Else

NextSchedule = Date + 1 + TimeSerial(0, 0, 0)

End If



Call cmbPingSystem_Click

Application.OnTime EarliestTime:=NextSchedule, Procedure:="AutoPing", Schedule:=True




End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
in thisworkbook
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
     Reset_AutoPing
End Sub

in your module
VBA Code:
Global dNextSchedule                                            'Global variable, is in memory, so you can recall him -> on top of the module !!!

Sub Reset_AutoPing()
     On Error Resume Next                                       'normally this goes wrong, thus use the OnError to proceed
     Application.OnTime dNextSchedule, "AutoPing", , 0          'reset your schedule because of that parameter 0
End Sub

Sub AutoPing()

     Reset_AutoPing                                             'normally you're here at moment dNextSchedule, but for safety, do a Reset_AutoPing

     dNextSchedule = Date + TimeSerial(Hour(Now), Application.WorksheetFunction.Floor_Math(Minute(Time) + 5, 5), 0)
     Application.OnTime dNextSchedule, "AutoPing", , 1
     Beep
     'Call cmbPingSystem_Click

End Sub
 
Upvote 0
Solution
in thisworkbook
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
     Reset_AutoPing
End Sub

in your module
VBA Code:
Global dNextSchedule                                            'Global variable, is in memory, so you can recall him -> on top of the module !!!

Sub Reset_AutoPing()
     On Error Resume Next                                       'normally this goes wrong, thus use the OnError to proceed
     Application.OnTime dNextSchedule, "AutoPing", , 0          'reset your schedule because of that parameter 0
End Sub

Sub AutoPing()

     Reset_AutoPing                                             'normally you're here at moment dNextSchedule, but for safety, do a Reset_AutoPing

     dNextSchedule = Date + TimeSerial(Hour(Now), Application.WorksheetFunction.Floor_Math(Minute(Time) + 5, 5), 0)
     Application.OnTime dNextSchedule, "AutoPing", , 1
     Beep
     'Call cmbPingSystem_Click

End Sub
Thank you. It works.
But why can't I use Dim instead of Global?
 
Upvote 0
ww.youtube.com/watch?v=33JmyY83IpA
Dim is for variable within a procedure, while public and global are for variables valid within a whole procedure or even the whole project.

I wanted to memorise "dNextSchedule", the timestamp of the next time to restart the macro "AutoPing".
That timestamp has to be accessable for several procedures (macros) and remain in memory even after execution of the macro. So you have to declare it outside a macro and on top of a module.
I only used dNextSchedule within this module, so Public as enough, Global wasn't necessary.

dNextSchedule is created and refreshed every time "AutoPing" runs and remains its value even during the 4 minutes and 59 seconds of waiting for the next execution of "AutoPing".

Suppose you want to close the workbook, the annoying thing with "OnTime" is that it reopens the workbook at moment dNextSchedule. So, by calling "Reset_AutoPing" in the "Workbook_BeforeClose"-event, you prevent this.

Here you execute at every multiple of "5 minutes - 0 seconds" but suppose that was free and you run the macro "Auto_ping" now and a 2nd time 1 minute later. Unless that was the intention, but you have at that moment simultanious 2 moments of refreshing.
Suppose 1st run is moment 0, 2nd run is moment +1 minute, then the recalls 'll be at moment +5,+10, +15, +20, ... for the 1st and +6, +11, +16, +21, ... for the 2nd. That's why, as routine, as safety, the 1st line in "AutoPing"-macro is "Reset_AutoPing" to avoid multiple such simultanious runs.
 
Upvote 0
ww.youtube.com/watch?v=33JmyY83IpA
Dim is for variable within a procedure, while public and global are for variables valid within a whole procedure or even the whole project.

I wanted to memorise "dNextSchedule", the timestamp of the next time to restart the macro "AutoPing".
That timestamp has to be accessable for several procedures (macros) and remain in memory even after execution of the macro. So you have to declare it outside a macro and on top of a module.
I only used dNextSchedule within this module, so Public as enough, Global wasn't necessary.

dNextSchedule is created and refreshed every time "AutoPing" runs and remains its value even during the 4 minutes and 59 seconds of waiting for the next execution of "AutoPing".

Suppose you want to close the workbook, the annoying thing with "OnTime" is that it reopens the workbook at moment dNextSchedule. So, by calling "Reset_AutoPing" in the "Workbook_BeforeClose"-event, you prevent this.

Here you execute at every multiple of "5 minutes - 0 seconds" but suppose that was free and you run the macro "Auto_ping" now and a 2nd time 1 minute later. Unless that was the intention, but you have at that moment simultanious 2 moments of refreshing.
Suppose 1st run is moment 0, 2nd run is moment +1 minute, then the recalls 'll be at moment +5,+10, +15, +20, ... for the 1st and +6, +11, +16, +21, ... for the 2nd. That's why, as routine, as safety, the 1st line in "AutoPing"-macro is "Reset_AutoPing" to avoid multiple such simultanious runs.
Thank you very much. It is really clear and useful to me.?
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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