ערב טוב.
לאחר עדכון הAPI של בנק ישראל לקבלת שערי חליפין, יצרתי קוד VBA לקבלת הנתונים העדכניים.
מצורף לתועלת הציבור, וכמובן אשמח לכל הערה!
Option Compare Database
Option Explicit
Public Enum StrCurr
USD = 1
GBP = 2
SEK = 3
CHF = 4
CAD = 5
NZD = 6
DKK = 7
SGD = 8
HKD = 9
ZAR = 10
AUD = 11
EUR = 12
JOD = 13
NOK = 14
JPY = 15
RUB = 16
PLN = 17
MXN = 18
CZK = 19
TRY = 20
LBP = 21
EGP = 22
HUF = 23
INR = 24
CNY = 25
End Enum
Public Type Rate
Sum As Double
CuDate As Date
End Type
Public Function test()
Dim Rates As Rate
Rates = GetExchangeRates
With Rates
Debug.Print .CuDate
Debug.Print .Sum
End With
End Function
Public Function GetExchangeRates(Optional Dtdate As Date, Optional StrCurr As StrCurr = USD) As Rate
Dim str As String
Dim Json As Object
Dim item As Variant
Dim element As Variant
Dim CurrName As String
Dim Sum As Double
Dim i As String
If Dtdate = "00:00:00" Then Dtdate = Date
Select Case StrCurr
Case 1: CurrName = "USD"
Case 2: CurrName = "GBP"
Case 3: CurrName = "SEK"
Case 4: CurrName = "CHF"
Case 5: CurrName = "CAD"
Case 6: CurrName = "NZD"
Case 7: CurrName = "DKK"
Case 8: CurrName = "SGD"
Case 9: CurrName = "HKD"
Case 10: CurrName = "ZAR"
Case 11: CurrName = "AUD"
Case 12: CurrName = "EUR"
Case 13: CurrName = "JOD"
Case 14: CurrName = "NOK"
Case 15: CurrName = "JPY"
Case 16: CurrName = "RUB"
Case 17: CurrName = "PLN"
Case 18: CurrName = "MXN"
Case 19: CurrName = "CZK"
Case 20: CurrName = "TRY"
Case 21: CurrName = "LBP"
Case 22: CurrName = "EGP"
Case 23: CurrName = "HUF"
Case 24: CurrName = "INR"
Case 25: CurrName = "CNY"
Case Else: CurrName = ""
End Select
Select Case CurrName
Case ""
MsgBox "קוד מטבע לא חוקי!", vbCritical + vbMsgBoxRtlReading + vbMsgBoxRight
Case Else
str = "https://edge.boi.gov.il/FusionEdgeServer/sdmx/v2/data/dataflow/BOI.STATISTICS/EXR/1.0/RER_" & CurrName & "_ILS?startperiod=" & Format(Dtdate - 7, "YYYY-MM-DD") & "&endperiod=" & Format(Dtdate, "YYYY-MM-DD") & "&format=sdmx-json"
With CreateObject("MSXML2.ServerXMLHTTP.6.0")
.Open "GET", str, False
.send
str = .responseText
End With
Set Json = JsonConverter.ParseJson(str)
For Each item In Json("data")("dataSets")(1)("series")("0:0:0:0:0:0")("observations")
GetExchangeRates.Sum = Json("data")("dataSets")(1)("series")("0:0:0:0:0:0")("observations")(item)(1)
Next item
For Each item In Json("data")("structure")("dimensions")("observation")(1)("values")
GetExchangeRates.CuDate = item("name")
Next item
End Select
End Function