VBA to send API Get with cell reference

yits05

Board Regular
Joined
Jul 17, 2020
Messages
56
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have the following code which passes a single static API GET:

VBA Code:
Sub Test()

    Dim objHTTP As Object

    Dim MyScript As Object



    Set objHTTP = CreateObject("MSXML2.XMLHTTP")

    URL = "https://sample-api.abcdef.com/data/query=123456"

    

    objHTTP.Open "GET", URL, False

    objHTTP.send

    Set MyScript = CreateObject("MSScriptControl.ScriptControl")

    MyScript.Language = "JScript"

    

    Set RetVal = MyScript.Eval("(" + objHTTP.responsetext + ")")

    MsgBox RetVal.USD

End Sub

However, I would like the VBA to loop through a list of parameters in sheet1, cells a2:a22 and replace the end of the URL ( query=123456) is with the content of each of these cells. Then, I would it to paste the results from each query in the same range in sheet2 (i.e. a2:a22).

Thank you!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
VBA Code:
Sub Test()
    Dim objHTTP As Object
    Dim MyScript As Object

    Set objHTTP = CreateObject("MSXML2.XMLHTTP")
    For Row = 2 To 22
        URL = "https://sample-api.abcdef.com/data/query=" & Trim(Sheets("Sheet1").Cells(Row, 1))

        objHTTP.Open "GET", URL, False
        objHTTP.send
    
        Set MyScript = CreateObject("MSScriptControl.ScriptControl")
    
        MyScript.Language = "JScript"
    
        Set RetVal = MyScript.Eval("(" + objHTTP.responsetext + ")")
        Sheets("Sheet2").Cells(Row, 1) = RetVal.USD
    Next Row
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top