Passing a Date Argument to OnTime

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,253
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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,118
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Try enclosing the date arg in # symbols and make sure to format it US style.
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,253
Office Version
  1. 2016
Platform
  1. Windows
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,118
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
It should just be h:mm:ss format. Alternatively can you pass it as a straight Double?
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,253
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,118
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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
 
Solution

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,253
Office Version
  1. 2016
Platform
  1. Windows
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.
 

Forum statistics

Threads
1,136,444
Messages
5,675,895
Members
419,591
Latest member
mersanko

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