VBA to send API Get with cell reference

yits05

New Member
Joined
Jul 17, 2020
Messages
44
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!
 

Some videos you may like

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,276
Office Version
  1. 2010
Platform
  1. Windows
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
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,127,831
Messages
5,627,147
Members
416,223
Latest member
RichardHell

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
Top