有关在函数

时间:2017-09-23 21:23:26

标签: vba function

我一直在谷歌搜索,但无法弄清楚我做错了什么。

    Public Sub ConnectToBloomberg(Code)

Dim sUrl As String
Dim rawJson As Dictionary
Dim pricedata As Object
Dim dataRequest As WinHttp.WinHttpRequest

sUrl = "http://www.bloomberg.com/markets/api/bulk-time-series/price/" & Code & "?timeFrame=1_YEAR"

Set dataRequest = New WinHttp.WinHttpRequest

With dataRequest
    .Open "GET", sUrl, True
    .Send
    .WaitForResponse
    FetchedData = .ResponseText
End With

 FetchedData = Right(FetchedData, Len(FetchedData) - 1)
 FetchedData = Left(FetchedData, Len(FetchedData) - 1)

Set Json = JsonConverter.ParseJson(FetchedData)

Set pricedata = Json.Item("price")

End Sub

Function MEANVALUE() As Double

ConnectToBloomberg (Code)

For Each Item In pricedata

    Count = Count + 1

    YReturn = Item("value") - LastPrice

    LastPrice = Item("value")

    Total = Total + YReturn

Next

Mean = Total / Count

MEANVALUE = Mean

End Function

问题应该在第二个函数中,我尝试调用Sub。

我对VBA很不错! : - )

我做错了什么?

谢谢!

以另一种方式解释。这段代码实际上有效:

    Function MEANVALUE(Code) As Double

Dim sUrl As String
Dim rawJson As Dictionary
Dim pricedata As Object
Dim dataRequest As WinHttp.WinHttpRequest

sUrl = "http://www.bloomberg.com/markets/api/bulk-time-series/price/" & Code & "?timeFrame=1_YEAR"

Set dataRequest = New WinHttp.WinHttpRequest

With dataRequest
    .Open "GET", sUrl, True
    .Send
    .WaitForResponse
    FetchedData = .ResponseText
End With

 FetchedData = Right(FetchedData, Len(FetchedData) - 1)
 FetchedData = Left(FetchedData, Len(FetchedData) - 1)

Set Json = JsonConverter.ParseJson(FetchedData)

Set pricedata = Json.Item("price")

For Each Item In pricedata

    Count = Count + 1

    YReturn = Item("value") - LastPrice

    LastPrice = Item("value")

    Total = Total + YReturn

Next

Mean = Total / Count

MEANVALUE = Mean

End Function

但我在其他功能中使用此代码:

Dim sUrl As String
Dim rawJson As Dictionary
Dim pricedata As Object
Dim dataRequest As WinHttp.WinHttpRequest

sUrl = "http://www.bloomberg.com/markets/api/bulk-time-series/price/" & Code & "?timeFrame=1_YEAR"

Set dataRequest = New WinHttp.WinHttpRequest

With dataRequest
    .Open "GET", sUrl, True
    .Send
    .WaitForResponse
    FetchedData = .ResponseText
End With

 FetchedData = Right(FetchedData, Len(FetchedData) - 1)
 FetchedData = Left(FetchedData, Len(FetchedData) - 1)

Set Json = JsonConverter.ParseJson(FetchedData)

Set pricedata = Json.Item("price")

所以我希望能够调用它,而不是在每个函数中手动使用代码。

1 个答案:

答案 0 :(得分:1)

您当前的ConnectToBloomberg子例程实际上并没有做任何有效的事情。它所做的所有计算都在子程序结束时丢失。

您有几种方法可以解决此问题:

  1. 使pricedata成为模块级范围变量,以便MEANVALUE函数可以访问它以及ConnectToBloomberg子例程。
  2. 通过调用pricedata
  3. 中的参数返回ConnectToBloomberg
  4. ConnectToBloomberg更改为Function(返回价格数据)而不是子程序。
  5. 以下是使用第三种方法的一些代码:

    Public Function ConnectToBloomberg(Code As String) As Object
    
        Dim sUrl As String
        Dim rawJson As Dictionary
        Dim dataRequest As WinHttp.WinHttpRequest
        Dim Json As Object
        Dim FetchedData As String
    
        sUrl = "http://www.bloomberg.com/markets/api/bulk-time-series/price/" & Code & "?timeFrame=1_YEAR"
    
        Set dataRequest = New WinHttp.WinHttpRequest
    
        With dataRequest
            .Open "GET", sUrl, True
            .Send
            .WaitForResponse
            FetchedData = .ResponseText
        End With
    
        FetchedData = Right(FetchedData, Len(FetchedData) - 1)
        FetchedData = Left(FetchedData, Len(FetchedData) - 1)
    
        Set Json = JsonConverter.ParseJson(FetchedData)
    
        Set ConnectToBloomberg = Json.Item("price")
    End Sub
    
    Function MEANVALUE(Code As String) As Double
        Dim pricedata As Object
        Dim Item As Variant
        Dim Count As Long
        Dim YReturn As Double
        Dim LastPrice As Double
        Dim Total As Double
    
        Set pricedata = ConnectToBloomberg(Code)
    
        For Each Item In pricedata
            Count = Count + 1
            YReturn = Item("value") - LastPrice
            LastPrice = Item("value")
            Total = Total + YReturn
        Next
        If Count > 0 Then
            MEANVALUE = Total / Count
        Else
            MEANVALUE = 0
        End If
    End Function