简体   繁体   中英

Excel 2010 replacing WEBSERVICE() with VBA

I have a spreadsheet that converts USD into EURO.

I designed it with Excel 2013 and I use the new function WEBSERVICE to retrieve historical exchange rates from internet, API here:

http://api.fixer.io/2017-01-07?base=USD&symbols=EUR

And my formula look like below:

=IF(OFFSET(INDIRECT("R[0]C",),0,-2)<>"",SUBSTITUTE(MID(WEBSERVICE("http://api.fixer.io/"&TEXT(EOMONTH(OFFSET(INDIRECT("R[0]C",),0,-2),0),"yyyy-mm-dd")&"?base=USD&symbols=CNY"),50,6),"}",""),"")

Explain: If the second cell to the left of the current cell is not empty, then fill it with the web data. The parameter in the formula is the month end date, also calculated by a cell in this table.

The table has multiple rows, and this is working perfectly for us, only problem is that some users are still on Excel 2010 or older, is there a way of doing this in VBA?

The simplest version of a VBA WEBSERVICE equivalent would be:

Public Function myWEBSERVICE(strURL As String) As String
 Dim oWinHttp As Object

 Set oWinHttp = CreateObject("MSXML2.XMLHTTP")

 oWinHttp.Open "GET", strURL, False
 oWinHttp.send ""

 myWEBSERVICE = oWinHttp.ResponseText

End Function

Used as UDF (User Defined Function) as a cell formula like:

=mywebservice("http://api.fixer.io/"&TEXT(A1,"yyyy-mm-dd")&"?base=USD&symbols=CNY")

where A1 contains a date.

For a reference see responseText Property (IXMLHTTPRequest) . Examples there are in JScript and must be converted to VBA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM