将字典传递给Application.OnTime

时间:2015-10-07 10:14:13

标签: vba dictionary ontime

我想将Scripting.Dictionary对象传递给Application.OnTime。 问题是我不断收到编译错误:参数不是可选的'。

代码的结构如下:

Public Sub mainSub()

    Dim orderBookIndex As New Scripting.Dictionary
    Dim contractIndex As New Scripting.Dictionary

    Call computeStuff(0, orderBookIndex, contractIndex)

End Sub

mainSub调用computeStuff,然后调用computeStuff调用' callLive'在增加后每秒启动一次。

Public Sub computeStuff(index As Integer, Optional orderBookIndex As Scripting.Dictionary, Optional contractIndex As Scripting.Dictionary)

    ' Do stuff

    If index = 0 Then
        Set orderBookIndex = New Scripting.Dictionary
        Set contractIndex = New Scripting.Dictionary
    End If

    If index = 1 Then

       Set orderBookIndex = New Scripting.Dictionary
       Set contractIndex = New Scripting.Dictionary

       orderBookIndex.add "a", "b" ' add random stuff

       contractIndex.add "c", "d" ' add random stuff

    End If

    Call callLive(index, orderBookIndex, contractIndex)

End Sub

然后调用callLive并拒绝编译:

Public Sub callLive(index As Integer, Optional orderBookIndex As Scripting.Dictionary, Optional contractIndex As Scripting.Dictionary)

    Dim SchTime As Double

    SchTime = Now + TimeSerial(0, 0, 1)
    index = index + 1
    Application.OnTime SchTime, "'computeStuff """ & index & """ ,""" & orderBookIndex & """ ,""" & contractIndex & """'", , False

End Sub

任何帮助或指导来完成此任务(或解决此问题)都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

尝试将orderBookIndex和contractIndex作为全局变量,如下所示,看看它是否适合您:

Public orderBookIndex As Scripting.Dictionary
Public contractIndex As Scripting.Dictionary

Public Sub mainSub()

Set orderBookIndex = New Scripting.Dictionary
Set contractIndex = New Scripting.Dictionary

Call computeStuff(0, orderBookIndex, contractIndex)

End Sub
Public Sub computeStuff(index As Integer, Optional orderBookIndex As Scripting.Dictionary, Optional contractIndex As Scripting.Dictionary)

' Do stuff

If index = 0 Then
    Set orderBookIndex = New Scripting.Dictionary
    Set contractIndex = New Scripting.Dictionary
End If

If index = 1 Then

   Set orderBookIndex = New Scripting.Dictionary
   Set contractIndex = New Scripting.Dictionary

   orderBookIndex.Add "a", "b" ' add random stuff

   contractIndex.Add "c", "d" ' add random stuff

End If

Call callLive(index, orderBookIndex, contractIndex)

End Sub

Public Sub callLive(index As Integer, orderBookIndex As Scripting.Dictionary, contractIndex As Scripting.Dictionary)

Dim SchTime As Double

SchTime = Now + TimeSerial(0, 0, 1)
index = index + 1
Application.OnTime SchTime, "'computeStuff " & index & " , orderBookIndex, contractIndex'"

End Sub