VBA - Inkludere kode i en function
Hej Eksperter,Jeg har denne kode:
[code]
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
[/code]
Jeg bruger den første stykke koder i andre funktioner også:
[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")
[/code]
Hvordan kan jeg ligesom fjerne ovenstående kode i funktionen og så inkluderer koden?
Kan man lave den om til en sub og så kalde den i funktionen eller hvorledes?