简体   繁体   English

Excel 2010用VBA替换WEBSERVICE()

[英]Excel 2010 replacing WEBSERVICE() with VBA

I have a spreadsheet that converts USD into EURO. 我有一个电子表格,将USD转换为EURO。

I designed it with Excel 2013 and I use the new function WEBSERVICE to retrieve historical exchange rates from internet, API here: 我使用Excel 2013设计它,并使用新功能WEBSERVICE从Internet,API此处检索历史汇率:

http://api.fixer.io/2017-01-07?base=USD&symbols=EUR 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. 说明:如果当前单元格左侧的第二个单元格不为空,则用Web数据填充它。 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? 该表有多行,对我们来说这是完美的工作,唯一的问题是某些用户仍在使用Excel 2010或更早版本,是否可以在VBA中执行此操作?

The simplest version of a VBA WEBSERVICE equivalent would be: 等效的VBA WEBSERVICE的最简单版本是:

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: 用作UDF(用户定义函数)作为单元格公式,例如:

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

where A1 contains a date. 其中A1包含一个日期。

For a reference see responseText Property (IXMLHTTPRequest) . 有关参考,请参见responseText属性(IXMLHTTPRequest) Examples there are in JScript and must be converted to VBA. JScript中提供了示例,必须将其转换为VBA。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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