Passing a Date Argument to OnTime

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,616
Office Version
  1. 2016
Platform
  1. Windows
It seems that passing a Date arg to the OnTime Method doesn't work... Any ideas ?

VBA Code:
Option Explicit

Private dNextRun As Date

Private Sub Workbook_Open()
    RunPeriodicMacro Interval:=TimeValue("00:00:02")  'run every 2 secs.
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnTime dNextRun, "'" & Me.CodeName & ".RunPeriodicMacro " & 0 & "'", Schedule:=False
End Sub

Private Sub RunPeriodicMacro(ByVal Interval As Date) 
    dNextRun = Now + Interval 
    Debug.Print "NextRun @: ", dNextRun 
    Application.OnTime dNextRun, "'" & Me.CodeName & ".RunPeriodicMacro " & Interval & "'", Schedule:=True
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try enclosing the date arg in # symbols and make sure to format it US style.
 
Upvote 0
Try enclosing the date arg in # symbols and make sure to format it US style.
Thanks but I couldn't figure out how to pass the litteral time value to the sub ... the compiler keeps changing the value.
I know the US date format is mm/dd/yy but I am not sure about the US time format .
Thanks.
 
Upvote 0
It should just be h:mm:ss format. Alternatively can you pass it as a straight Double?
 
Upvote 0
It should just be h:mm:ss format. Alternatively can you pass it as a straight Double?
Initially, I declared the interval as a Double and it worked but, out of interest, I want to know why I can't make it work with a Date type argument.

BTW, the VBE automatically changes the argument #00:00:02# to #12:00:02 AM# for both as Double or as Date which is understandable because litteral time is not a time interval value it is an actual clock time. However, the resulting format is counter-intuitive

The following works without the need to use litteral '#' time because the argument is declared as Double.
VBA Code:
Private Sub Workbook_Open()
    RunPeriodicMacro Interval:=TimeValue("00:00:02")
End Sub

Private Sub RunPeriodicMacro(Interval As Double)
    dNextRun = Now + Interval
    Debug.Print "NextRun @: " & vbTab & dNextRun
    Application.OnTime dNextRun, "'" & Me.CodeName & ".RunPeriodicMacro " & Interval & "'", Schedule:=True
End Sub

Declaring the argument as Date type still doesn't work for me even when enclosing it in # symbols.
 
Upvote 0
This works for me:

VBA Code:
Option Explicit

Private dNextRun As Date

Private Sub Workbook_Open()
    RunPeriodicMacro Interval:=TimeValue("00:00:02")  'run every 2 secs.
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnTime dNextRun, "'" & Me.CodeName & ".RunPeriodicMacro " & 0 & "'", Schedule:=False
End Sub

Private Sub RunPeriodicMacro(ByVal Interval As Date)
    dNextRun = Now + Interval
    Debug.Print "NextRun @: ", dNextRun
    Application.OnTime dNextRun, "'" & Me.CodeName & ".RunPeriodicMacro #" & Interval & "#'", Schedule:=True
End Sub
 
Upvote 0
Solution
Yes! That works.

I was actually enclosing the argument when calling the sub:
RunPeriodicMacro Interval:=#12:00:02 AM#
When passed to the argument variable, the "#" symbols are removed that's why it didn't work.

Thank you for your help.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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