Application.OnTime如何引用带参数的过程
Application.OnTime方法本身并不直接支持传递参数给被调用的过程。不过,有几种方法可以间接实现这个需求。
方法1:使用单引号表达式
使用单引号表达式来传递参数时,不能在表达式中使用变量,需要把参数值直接写到表达中,注意在表达中一个双引号需要用两个来表示,例如:
Sub test()
Application.OnTime Now + TimeValue("00:00:00"), "'MySub ""Hellow! "",""Jim""'"
End Sub
Sub MySub(strA As String, strB As String)
MsgBox strA & vbCrLf & strB
End Sub
方法2:使用全局变量
你可以使用全局变量来传递参数,例如:
Public strA As String, strB As String
Sub test()
strA = "Hellow! "
strB = "Jim"
Application.OnTime Now + TimeValue("00:00:00"), "MySub"
End Sub
Sub MySub()
MsgBox strA & vbCrLf & strB
End Sub
方法3:使CALL方法
使用CALL方法来传递参数的需要新增一个子过程,例如:
Sub test()
Application.OnTime Now + TimeValue("00:00:00"), "MyVar"
End Sub
Sub MyVar()
Dim strA As String, strB As String
strA = "Hellow! "
strB = "Jim"
Call MySub(strA, strB)
End Sub
Sub MySub(strA As String, strB As String)
MsgBox strA & vbCrLf & strB
End Sub
以上方法选择哪一种取决于你的具体需求和场景。