2015-03-19 48 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