Excel VBA从url下载文件

我正在尝试构build一个Excel VBA命令,它将自动执行以下操作:

  1. 从已经制作好的excel电子表格中导入另一个excel wordbook的列表,其中的项目将与相应的列匹配(即识别地址,邮政编码等,并将它们分配到包含地址,邮政编码等的列)
  2. 从邮编列表(列)复制粘贴子9,999行,并在线粘贴到以下URL( http://imd-by-postcode.opendatacommunities.org/ ))
  3. 然后,将这些值粘贴到文本框区域后,我想按下“获取剥夺数据”button,然后下载创build的xlsx文件
  4. 下载的Excel工作簿将被打开,所有信息将复制/粘贴到我的原始工作簿上的特定空白工作表中
  5. 重复此function,直到所有子9999行都完成(即如果我有3万行的数据这样做了3次)

到目前为止,我已经开始做的是第2步和第3步,我已经设法复制粘贴到文本框区域的值,并“按”获取剥夺数据“button,但我堆放在如何下载生成的xlsx由下一页上的网站创build的文件。 我正在使用的代码如下,但是在下载文件时我总是收到错误:

Sub papafi_1_command() Dim ie As Object Dim MyURL As String Set ie = CreateObject("InternetExplorer.Application") 'create new instance of IE. use reference to return current open IE if 'you want to use open IE window. Easiest way I know of is via title bar. MyURL = "http://imd-by-postcode.opendatacommunities.org/" ie.Navigate MyURL 'go to web page listed inside quotes ie.Visible = True While ie.Busy DoEvents 'wait until IE is done loading page. Wend 'Generate text string Dim str As String Dim arr() As Variant Dim tableRow As Integer Dim tableCol As Integer 'Assign range to an array arr = Range("C7:C10005") Sheets("RPInputs").Range("C7:C10005").Select Selection.Copy 'Loop through each row of the range to format a tab delimited text string For tableRow = LBound(arr) To UBound(arr) For tableCol = LBound(arr, 2) To UBound(arr, 2) str = str & arr(tableRow, tableCol) & vbTab Next tableCol str = str & vbNewLine Next tableRow With ie.Document 'Assign text string to textarea .getElementById("postcodes").Value = str 'Timedelay Application.Wait (Now + TimeValue("0:00:03")) 'Button Click .getElementById("submit").Click 'Timedelay #2 Application.Wait (Now + TimeValue("0:00:30")) 'Download Dim strURL As String Dim strPath As String Dim strString As String Dim iEnd As Long Dim iStart As Long strString = "Visit my webpage at http://imd-by-postcode.opendatacommunities.org/" 'the string you want to search iStart = InStrRev(strString, "http") 'This is where your url starts iEnd = InStrRev(strString, ".com") 'This is where your url ends strURL = Mid(strString, iStart, (iEnd - iStart) + 4) '~~> URL of the Path 'strURL = "http://imd-by-postcode.opendatacommunities.org/" ' ~~> Destination for the file strPath = "C:\USers\FilipposP\Downloads\deprivation-by-postcode (test).xlsx" ret = URLDownloadToFile(0, FieldStart, strPath, 0, 0) If ret = 0 Then MsgBox "File successfully downloaded" Else MsgBox "Unable to download the file" End If End With End Sub 

我有兴趣在pipe理下载文件,但是,如果你能帮助,并在步骤1或5,将不胜感激。