Using Application.OnTime

Lavina

Board Regular
Joined
Dec 18, 2018
Messages
75
Hello guys,

I'm trying to setup a timeout for a macro I'm creating, it reaches out to a file, if that file is open it times out for 10 sec and then tries again.
My initial approach was to make a do loop for x seconds, but that was an awful idea because it prevents other actions from happening.

My second approach landed me on: Application.OnTime.

After reading the documentation: https://docs.microsoft.com/en-us/office/vba/api/excel.application.ontime
I was surprised that: "Procedure[FONT=&quot] must take no arguments and cannot be declared in a custom class or form."
Why is it so? What is the normal work around?

Calling a sub that calls a sub with arguments just [/FONT]
feels incompetent.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You can pass scalar arguments, it's just a little fussy:

Code:
Sub main()
  Range("A1").Value = "Bob"
  Application.OnTime EarliestTime:=Now(), Procedure:=CallString("mySub", "Test", "A1")
End Sub

Sub mySub(arg1 As String, arg2 As String)
  MsgBox arg1 & " " & Range(arg2).Value
End Sub

Function CallString(sProc As String, ParamArray av() As Variant) As String
  ' shg 2014

  Dim i             As Long

  If UBound(av) = -1 Then
    CallString = sProc
  Else
    CallString = "'" & sProc & " "
    For i = 0 To UBound(av)
      If VarType(av(i)) = vbString Then av(i) = """" & av(i) & """"
    Next i
    CallString = CallString & Join(av, ", ") & "'"
  End If
End Function
 
Upvote 0

Forum statistics

Threads
1,214,851
Messages
6,121,931
Members
449,056
Latest member
denissimo

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