Passing multiple times to Application.OnTime Method

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
516
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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,215,096
Messages
6,123,074
Members
449,093
Latest member
ripvw

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