VBA – 如何从excel文件中获取数据,并将其下载到当前的excel文件中

我有这个代码,下载一个Excel文件,但我怎么会把数据从这个文件传输到我运行macros的Excel文件。 你会注意到,这个网站提供了一个URL直接下载文件,但我想弄清楚如何做到这一点,没有去那条路由,因为另一个项目需要它。

任何帮助将不胜感激。

Sub GetData() Dim IE As InternetExplorer Dim HTMLDoc As HTMLDocument Dim objElement As HTMLObjectElement Set IE = New InternetExplorer With IE .Visible = True .Navigate "http://www.housepriceindex.ca/default.aspx" While .Busy Or .ReadyState <> READYSTATE_COMPLETE: Wend .Document.getElementById("lnkTelecharger2").Click While .Busy Or .ReadyState <> READYSTATE_COMPLETE: Wend Set HTMLDoc = .Document Set objElement = HTMLDoc.getElementById("txtEmailDisclaimerEN") objElement.Value = "Email Address" Set objElement = HTMLDoc.getElementById("lnkAcceptDisclaimerEN") objElement.Click ' ... Get CSV somehow ... .Quit End With Set IE = Nothing End Sub 

我没有看到该网站上的任何types的CSV,但我想你知道它在哪里。 只需资助它,并将实际的url映射到下面的示例url。

 Sub DownloadFileWithVBA() Dim myURL As String 'Right-click on the link named 'Sample Address File' 'Click 'Copy Link Location' 'Paste the link below myURL = "http://databases.about.com/library/samples/address.xls" Dim WinHttpReq As Object Set WinHttpReq = CreateObject("Microsoft.XMLHTTP") WinHttpReq.Open "GET", myURL, False WinHttpReq.Send myURL = WinHttpReq.ResponseBody Set oStream = CreateObject("ADODB.Stream") oStream.Open oStream.Type = 1 oStream.Write WinHttpReq.ResponseBody oStream.SaveToFile ("C:\your_path\address.xls") oStream.Close End Sub