VBA:我想将私有类模块变量值传递给公共变量

时间:2018-06-07 15:00:08

标签: vba

我有来自数据提供程序的API,它使用类模块发送数据请求,然后处理请求。我在类模块的最后进程代码(TempTickers)中有一个字符串数组,并希望将此数组传递给现有的公共变​​量(Tickers)。

模块

Option Explicit

Dim bbControl As New BB_API_Port

Public f1Ticker, f2Ticker, f1Type, f2Type As String
Public Tickers() As String

Sub BB_Pull_Portfolio()

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

ReDim sSecurity(0 To 0) As String
ReDim sFields(0 To 0) As Variant
ReDim Tickers(0 To 0) As String

sSecurity(0) = f1Ticker
sFields(0) = "PORTFOLIO_MPOSITION"
sOverrideField = "REFERENCE_DATE"
sOverrideValue = Format(Range("Date_1").Value, "YYYYMMDD")

bbControl.PortRequest sSecurity, sFields, sOverrideField, sOverrideValue

End Sub

课程模块BB_API_Port

Option Explicit

Private WithEvents Port_session As blpapicomLib2.session
Dim refdataservice As blpapicomLib2.Service

Dim subfieldArray As Variant, FileName As String
Dim curRow As Integer

' Instantiate the Bloomberg COM Control
Private Sub Class_Initialize()

Set Port_session = New blpapicomLib2.session
Port_session.QueueEvents = True
Port_session.Start

Port_session.OpenService ("//blp/refdata")
Set refdataservice = Port_session.GetService("//blp/refdata")

End Sub

'Destroy the Bloomberg COM Control
Private Sub Class_Terminate()

Set Port_session = Nothing

End Sub

Public Sub PortRequest(sSecList() As String, sFldList As Variant, sOverrideField As String, sOverrideValue As String)

Dim req As Request
Dim nRow As Long

Set req = refdataservice.CreateRequest("PortfolioDataRequest")
For nRow = LBound(sSecList, 1) To UBound(sSecList, 1)
    req.GetElement("securities").AppendValue (sSecList(nRow))
Next
For nRow = LBound(sFldList, 1) To UBound(sFldList, 1)
    req.GetElement("fields").AppendValue (sFldList(nRow))
Next

If sOverrideValue <> "" Then
    Dim overrides As Element
    Set overrides = req.GetElement("overrides")
    Dim override  As Element
    Set override = overrides.AppendElment()
    override.SetElement "fieldId", sOverrideField
    override.SetElement "value", sOverrideValue
End If

Dim cid As blpapicomLib2.CorrelationId
Set cid = Port_session.SendRequest(req)

End Sub

Private Sub Port_session_ProcessEvent(ByVal obj As Object)

On Error Resume Next

Dim eventObj As blpapicomLib2.Event
Set eventObj = obj
Dim iFund As Worksheet
Dim TempTickers() As String

Set iFund = Fund1

If Application.Ready Then
    If eventObj.EventType = PARTIAL_RESPONSE Or eventObj.EventType = RESPONSE Then
        Dim it As blpapicomLib2.MessageIterator
        Set it = eventObj.CreateMessageIterator()

        Do While it.Next()

            Dim NumItems As Integer
            NumItems = it.Message.GetElement("securityData").GetValue(0).GetElement("fieldData").GetElement(0).NumValues
            Dim Item As blpapicomLib2.Element
            Set Item = it.Message.GetElement("securityData").GetValue(0).GetElement("fieldData").GetElement(0)

            Dim i, n As Integer
            n = 0
            For i = 0 To NumItems - 1
                If Right(Item.GetValue(i).GetElement(0).Value, 6) = "Equity" Then
                    iFund.Cells(i + 2, 1).Value = Item.GetValue(i).GetElement(0).Value
                    iFund.Cells(i + 2, 2).Value = Item.GetValue(i).GetElement(1).Value
                    If n = 0 Then
                        ReDim TempTickers(0 To 0) As String
                    Else
                        ReDim TempTickers(0 To UBound(TempTickers()) + 1) As String
                    End If
                    TempTickers(n) = Item.GetValue(i).GetElement(0).Value
                    n = n + 1
                End If
            Next i
        Loop
    End If
End If

End Sub

0 个答案:

没有答案