Passing multiple times to Application.OnTime Method

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
558
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I am trying to loop the Applicaton.OnTime method through multiple values in a sheet.

The aim is to set up multiple Application.OnTime events.

The variable times are downloaded from a third party site and are being read in from cells so they can't be hard coded.

The OnTime's are firing off ok and with the right formats, however, only the last StartTime is being displayed in the msgbox because that is the last StartTime that has been created.

I assume that I should be using an array structure to create a StartTime variable and then pass that variable to the msgbox. But arrays are not my strong suit.

Much appreciated if someone can point me in the right syntax direction.

Cheers

VBA Code:
Sub Time_Value_Message_Box()
Dim StartTime As Date

For ii = 1 To 3
    StartTime = Sheet10.Range("A" & ii).Value
    Application.OnTime StartTime, "Time_Value_MSG"
Next ii
End Sub

Sub Time_Value_MSG()
    MsgBox "Current Start Time is: " & StartTime
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).
I would pass the StartTime variable as an argument to the OnTime Method. I would even pass the caller cell... No need to use an array .
VBA Code:
Sub Time_Value_Message_Box()
    Dim StartTime As Date, ii As Long, sAddrss As String
    For ii = 1& To 3&
        With Sheet10.Range("A" & ii)
            StartTime = .Value
            sAddrss = .Address(0&, 0&)
        End With
        Application.OnTime StartTime, "'Time_Value_MSG """ & sAddrss & """,""" & StartTime & "'"
    Next ii
End Sub

Sub Time_Value_MSG(ByVal CellAddress As String, ByVal StartTime As Date)
    MsgBox "Caller Cell is: [" & CellAddress & "]" & vbLf & _
           "Current Start Time is: " & Format(StartTime, "hh:mm:ss")
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,929
Messages
6,175,451
Members
452,642
Latest member
acarrigan

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