2015-03-19 46 views
0

检索数据,我需要建立一个从网页像这样的检索数据的Excel VBA用户定义函数:最好的办法建立一个VBA UDF从网站

https://www.comdinheiro.com.br/Clientes/ServerToExcel/S2E_TESTANDO001.php?func1=retorno&func2=retorno(01/01/2011,16/03/2015,ptaxc,todos)

我知道我可以创造一个查询每个函数的调用,但这会导致工作表上的查询太多。我也可以打开一个IE浏览器并“读取”html脚本来获取数据,但这需要很长时间。那么,我错过解决这个问题的其他可能方式是什么?

谢谢!

回答

2

不知道这是否是最好的方法,但可以使用MSXML2.XMLHTTP对象来请求站点。

实施例:

enter image description here

式中B2向下是=getResult(A2,B2)

的UDF getResult是:

Public Function getResult(dFromDate As Date, dToDate As Date) As Double 

Dim sFromdate As String, sToDate As String 
sFromdate = WorksheetFunction.Text(dFromDate, "dd/mm/yyyy") 
sToDate = WorksheetFunction.Text(dToDate, "dd/mm/yyyy") 

Dim sURL As String, sArguments As String, sRequest As String 

sURL = "https://www.comdinheiro.com.br/Clientes/ServerToExcel/S2E_TESTANDO001.php" 
sArguments = "?func1=retorno&func2=retorno%28" & sFromdate & "," & sToDate & ",ptaxc,todos%29" 
sRequest = sURL & sArguments 

Dim httpObject As Object 
Set httpObject = CreateObject("MSXML2.XMLHTTP") 
httpObject.Open "GET", sRequest, False 
httpObject.send 

Dim sGetResult As String 
sGetResult = httpObject.responseText 
sGetResult = Replace(sGetResult, ".", Application.DecimalSeparator) 

getResult = CDbl(sGetResult) 

End Function