【问题标题】:Extracting a Specific Variable from a Class Module in VBA to a Standard Module从 VBA 中的类模块中提取特定变量到标准模块
【发布时间】:2013-04-17 21:51:46
【问题描述】:

所有, 以下代码来自彭博社。它旨在从他们的服务器中提取大量数据。该代码有效,但我试图提取在类模块中生成的特定变量并将其带到常规模块以用于用户定义的函数。感谢您的帮助。

Option Explicit
Private WithEvents session As blpapicomLib2.session
Dim refdataservice As blpapicomLib2.Service

Private Sub Class_Initialize()
Set session = New blpapicomLib2.session
session.QueueEvents = True
session.Start
session.OpenService ("//blp/refdata")
Set refdataservice = session.GetService("//blp/refdata")
End Sub

Public Sub MakeRequest(sSecList As String)
Dim sFldList As Variant
Dim req As Request
Dim nRow As Long
sFldList = "CALL_SCHEDULE"
Set req = refdataservice.CreateRequest("ReferenceDataRequest") 'request type
req.GetElement("securities").AppendValue (sSecList) 'security + field as string array
req.GetElement("fields").AppendValue (sFldList) 'field as string var
Dim cid As blpapicomLib2.CorrelationId
Set cid = session.SendRequest(req)
End Sub

Public Sub session_ProcessEvent(ByVal obj As Object)
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()
        Do While it.Next()
            Dim msg As Message
            Set msg = it.Message
                Dim Security As Element
                Set Security = msg.GetElement("securityData").GetValue(0)
                Sheet1.Cells(4, 4).Value = Security.GetElement("security").Value
                Dim fieldArray As Element
                Set fieldArray = Security.GetElement("fieldData")
                    Dim field As blpapicomLib2.Element
                    Set field = fieldArray.GetElement(0)
                    If field.DataType = 15 Then
                        Dim numBulkValues As Long
                        numBulkValues = field.NumValues '76
                        Dim index As Long
                        For index = 0 To numBulkValues - 1
                            Dim bulkElement As blpapicomLib2.Element
                            Set bulkElement = field.GetValue(index)
                            Dim numBulkElements As Integer
                            numBulkElements = bulkElement.NumElements '2 elements per each pt
                            ReDim Call_Sch(0 To numBulkValues - 1, 0 To numBulkElements - 1) As Variant
                            Dim ind2 As Long
                            For ind2 = 0 To numBulkElements - 1
                                Dim elem As blpapicomLib2.Element
                                Set elem = bulkElement.GetElement(ind2)
                                Call_Sch(index,ind2)=elem.Value
                                Sheet1.Cells(index + 4, ind2 + 5) = elem.Value
                            Next ind2
                        Next index
                    Else
                    Call_Sch(index,ind2)=field.Value    
                    Sheet1.Cells(index + 4, ind2 + 5).Value = field.Value
                    End If
        Loop
    End If
End If
End Sub

我想要获取的变量,具体来说,是 Call_Sch。我想要主模块中的一个函数来识别变量。再次感谢。

【问题讨论】:

  • 要使类中的值在外部可见,您可以将其设为属性,如下例所示:stackoverflow.com/questions/5342666/… 或者您可以只公开您的数组。但是您似乎没有发布所有代码,因为没有任何代码可以在任何地方声明 Call_Sch - 只有 ReDim 的代码。

标签: excel vba excel-2010 bloomberg


【解决方案1】:

在使用 ReDim 之前不必声明变量; ReDim 可以声明一个变量。但是,如果您添加:

Public Call_Sch() as Variant ' Insert correct data type here

那么您可以通过以下方式引用它:

<YourClassVaraibleName>.Call_Sch 

【讨论】:

    猜你喜欢
    • 2011-05-07
    • 1970-01-01
    • 2013-12-11
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多