VBA事件处理程序返回值

时间:2018-04-02 02:15:30

标签: vba function event-handling bloomberg

我有一个事件处理程序Sub,用于侦听来自Bloomberg API的响应消息,并将其存储在Dd数组中。

根据MSDN事件处理程序必须是,但我想对main方法中的数据进行进一步分析。

如何引用在事件处理程序中创建的数组,以便我可以继续处理数据?

事件处理程序:

Private Sub session_ProcessEvent(ByVal obj As Object)

    On Error GoTo errHandler

    Dim eventObj As blpapicomLib2.Event
    Set eventObj = obj

    If Application.Ready Then
        If eventObj.EventType = PARTIAL_RESPONSE Or eventObj.EventType = RESPONSE Then
            Dim it As blpapicomLib2.MessageIterator
            Set it = eventObj.CreateMessageIterator()
            Dim numResponse As Integer
            numResponse = 0
            Dim data() As Variant
            Do While it.Next()
                numResponse = numResponse + 1
                Dim msg As Message
                Set msg = it.Message
                Dim securityData As Element
                Dim securityName As Element
                Dim fieldData As Element
                Set securityData = msg.GetElement("securityData")
                Set securityName = securityData.GetElement("security")
                Set fieldData = securityData.GetElement("fieldData")
                Sheet1.Cells(currentRow, 4).Value = securityName.Value
                Dim numDates As Integer
                Dim numFields As Integer
                numDates = fieldData.NumValues
                numFields = fieldData.GetValue(0).NumElements
                ReDim data(numDates, numFields, numResponse)
                Dim b As Integer
                For b = 0 To numDates - 1
                    Dim fields As blpapicomLib2.Element
                    Set fields = fieldData.GetValue(b)
                    Dim a As Integer

                    For a = 0 To numFields - 1
                        Dim field As Element
                        Set field = fields.GetElement(a)
                        data(b, a, numResponse) = field.Value
                        Sheet1.Cells(currentRow, a + 5).Value = data(b, a, numResponse)
                    Next
                    currentRow = currentRow + 1
                Next b
            Loop

        '     skip a row for next security
            currentRow = currentRow + 1

        End If
    End If
    Exit Sub

errHandler:
    MsgBox Err.Description

End Sub

...而且我想在这里处理数组...

Public Sub RefDataExample()

    ' Calculate the number of securities and fields
    Dim numSecurity As Integer
    Dim numFields As Integer

    numSecurity = 0
    numFields = 0

    ' clear data area
    Range("D4", "H60000").Clear

    Do While Cells(numSecurity + 4, 1).Value <> ""
        numSecurity = numSecurity + 1
    Loop

    Do While Cells(numFields + 4, 2).Value <> ""
        numFields = numFields + 1
    Loop

    Dim sSecurity() As String
    Dim sFields() As Variant

    ReDim sSecurity(0 To numSecurity - 1) As String
    ReDim sFields(0 To numFields - 1) As Variant

    Dim i As Integer

    For i = 0 To numSecurity - 1
        sSecurity(i) = Cells(i + 4, 1).Value
    Next i

    For i = 0 To numFields - 1
        sFields(i) = Cells(i + 4, 2).Value
    Next i

    bbControl.MakeRequest sSecurity, sFields

    'Process response array here

End Sub

1 个答案:

答案 0 :(得分:0)

删除

Dim data() As Variant

来自当前所有潜艇中的内容(例如来自您session_ProcessEvent()

并放置

Public data As Variant

位于包含您的sub的模块顶部(实际上您可以将它放在您喜欢的任何模块的顶部)

您将从项目的所有子/功能中看到“数据”