I'm trying to retrieve data from an API but my variable doesn't update even if I set it as nothing before the GET request.
The data of the variable update only if I close Excel and re-open it.
Is there any explanation for it? I've been scratching my head for so long.
Here is the code
Sub getJsonResult()
Dim objRequestt As Object
Dim strUrl As String
Dim blnAsync As Boolean
Dim strUrlXBTUSD As String
Dim strResponse As String
Dim jsonText As String
Dim jsonObject As Object, item As Object
Dim i As Integer
'setting up the variable to 0 or nothing
strUrlXBTUSD = ""
strResponsee = ""
jsonText = ""
i = 0
blnAsync = False
Set item = Nothing
Set jsonObject = Nothing
Set objRequestt = Nothing
Set objRequestt = CreateObject("MSXML2.XMLHTTP")
strUrlXBTUSD = "https://www.bitmex.com/api/v1/orderBook/L2?symbol=XBTUSD&depth=3"
blnAsync = True
'Starting the GET request
ThisWorkbook.Activate
With objRequestt
.Open "GET", strUrlXBTUSD, blnAsync
.SetRequestHeader "Content-Type", "application/json"
.send
strResponse = .responseText 'here the response is always the same except if i Close Excel
Debug.Print strResponsee
End With
End Sub
At the end "strResponse" is always the same even after several F5 refresh. I can see that the data are no longer accurate on a web browser. I'd like the VBA program to get accurate data and refresh without closing Excel.
How to do that?