简体   繁体   English

使用 VBA 将数据从 Web 复制到 Excel

[英]Copy data from Web to excel using VBA

I have a webpage and need my code to copy the entire data from the page and copy it into the excel sheet, which is not happening right now.我有一个网页,需要我的代码来复制页面中的整个数据并将其复制到 Excel 表中,但现在没有发生这种情况。 My excel sheet is coming to be completely blank.我的 excel 表将完全空白。 I think the ^a feature is not working on the IE to select the data and then copy it.我认为^a功能不适用于 IE 来选择数据然后复制它。

Any help is much appreciated.任何帮助深表感谢。 Below is the code I am using.下面是我正在使用的代码。

Sub Webdata()

    Dim assetname As String, country As String, area As String, region As String, pth As String, folname As Variant, assetname1 As String

    Website = "http://website.com/"
    Set myIE = CreateObject("InternetExplorer.Application")
    myIE.Navigate source
    myIE.Visible = True
    Application.Wait Now + TimeSerial(0, 0, 10)
    SendKeys "^a"
    Application.Wait Now + TimeSerial(0, 0, 2)
    SendKeys "^c"
    Application.Wait Now + TimeSerial(0, 0, 2)
    Sheets.Add
    ActiveSheet.Name = "Webdata"
    ActiveSheet.Paste
    Application.Wait Now + TimeSerial(0, 0, 2)

    Range("A1").Select
    Cells.Find(What:="Api Number", After:=ActiveCell, LookIn:= _
               xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
               xlNext, MatchCase:=False, SearchFormat:=False).Activate

    ActiveCell.Offset(1, 0).Select
    Selection.Copy
    Sheets("Sheet1").Activate
    Range("C2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                           :=False, Transpose:=False
    Application.CutCopyMode = False
    myIE.Quit

    Set myIE = Nothing
    Err.Clear
    Sheets("Webdata").Select
    ActiveSheet.Delete

End Sub

That table is a mess so rather than spending time perfecting how to write out the table to the sheet in the way I normally would ie looping rows of tables and table cells within rows, I will stick with your idea of copying the table but use the clipboard, with .SetText , rather than SendKeys .那个表格是一团糟,所以我不会像通常那样花时间完善如何将表格写到工作表上,即在行内循环表格行和表格单元格,我会坚持你复制表格的想法,但使用剪贴板,带有.SetText ,而不是SendKeys The table of interest is within nested frames so you have to negotiate those first.感兴趣的表位于嵌套框架内,因此您必须先协商这些框架。

Set hTable = .frames(2).document.getElementsByTagName("table")(0)

Code:代码:

Option Explicit
Public Sub GetInfo()
    Dim IE As New InternetExplorer, html As HTMLDocument, hTable As HTMLTable, clipboard As Object
    Application.ScreenUpdating = False
    With IE
        .Visible = True
        .navigate "http://pipeline.wyo.gov/Wellapi.cfm?oops=IDxxxxx&nAPINO=xxxxxx" '<==Input your personal URL here 
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set html = .document
        With html
            Set hTable = .frames(2).document.getElementsByTagName("table")(0)
            Set clipboard = New MSForms.DataObject
            clipboard.SetText hTable.outerHTML
            clipboard.PutInClipboard
            ActiveSheet.Cells(1, 1).PasteSpecial
        End With
        .Quit
    End With
    Application.ScreenUpdating = True
End Sub

References:参考:

VBE> Tools > References: VBE> 工具> 参考资料:

  1. Microsoft Forms 2.0 Object Library Microsoft Forms 2.0 对象库
  2. HTML Object Library HTML 对象库
  3. Internet Explorer Controls Internet Explorer 控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM