从href下载文件并循环所有文件以保存在C驱动器的工作簿的单独选项卡中

时间:2019-04-21 07:27:24

标签: excel vba

尝试从href下载文件并通过循环将数据保存在C驱动器工作表的单独选项卡中。

感谢蒂姆·威廉姆斯(Tim Williams)帮助我在下载href文件之前放入代码。但是代码放置将下载的数据复制到excel。这对我不起作用。

Sub download()

    Dim ie As InternetExplorer
    Dim el As Object, els As Object

    Set ie = New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "http://www.nafed-india.com/Home/ProcDispoDetails"

        While .Busy Or .readyState < 4: DoEvents: Wend
    End With

    'Note: querySelectorAll unless you only want one element
    Set els = ie.document.querySelectorAll("a[href^='/Documents/ProcurementDisposal']")
    For Each el In els
        Debug.Print el.href 

    Dim i As Long

    Workbooks.Open Filename:="C:\Users\Desktop\Sample.xlsx"

    For i = 1 To ThisWorkbook.Sheets.Count
    Workbooks.Worksheets(i).Range("a1").Select
    ActiveCell.FormulaR1C1 = el.herf(i)
    Next i

    ActiveWorkbook.Save
    ActiveWindow.Close
        Next
End Sub

1 个答案:

答案 0 :(得分:0)

尝试类似这样的方法。 进行调整以适合您的实际情况。

Sub download()

    Dim ie As InternetExplorer, i As Long
    Dim el As Object, els As Object, wb As Workbook, smpl as workbook

    Set smpl = Workbooks.Open(Filename:="C:\Users\Desktop\Sample.xlsx")

    Set ie = New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "http://www.nafed-india.com/Home/ProcDispoDetails"

        While .Busy Or .readyState < 4: DoEvents: Wend
    End With

    'Note: querySelectorAll unless you only want one element
    Set els = ie.document.querySelectorAll("a[href^='/Documents/ProcurementDisposal']")
    i = 1
    For Each el In els
        Debug.Print el.href 
        Set wb = Workbooks.Open(el.href)
        wb.sheets(1).Range("A1").CurrentRegion.Copy smpl.Sheets(i).Range("A1")
        wb.close false
        i = i +1
   Next el

   smpl.Save

End Sub

注意:接受任何对您有帮助的答案都是有礼貌的-这可以帮助后来出现问题的人查看您的问题,以了解有效的答案。花几分钟来完成之前的文章-如果您忽略了自己,可能会发现帮助枯竭了...