【问题标题】:.Range("A" & i).Value = htmlEle.Children(0).textContent error.Range("A" & i).Value = htmlEle.Children(0).textContent 错误
【发布时间】:2021-01-08 20:56:36
【问题描述】:

Snapshot on the table i would like to get 我想从华尔街日报导入财务报表。我设计了一个代码(如下),但它似乎不起作用。当我运行它时,我得到“运行时错误 91 对象变量或未设置块变量”。单击调试后,带星号的行以黄色突出显示。

Can anyone assist?

Here is the code I wrote

Sub finanacialdata()
Dim ieobj As InternetExplorer
Dim htmlele As IHTMLElement
Dim i As Integer
i = 1
Set ieobj = New InternetExplorer
ieobj.Visible = True
ieobj.navigate ""https://www.wsj.com/market-data/quotes/GOL/financials/annual/balance-sheet
Application.Wait Now + TimeValue("00.00.10")

For Each htmlele In ie.obj.document.getElementsByClassName("zonedmodule")(0).getElementBytagname("tr")
    With ActiveSheet
         *.Range("A" & i).Value = htmlele.Children(0).textContent*
         .Range("B" & i).Value = htmlele.Children(1).textContent
         .Range("C" & i).Value = htmlele.Children(2).textContent
         .Range("D" & i).Value = htmlele.Children(3).textContent
         .Range("E" & i).Value = htmlele.Children(4).textContent
         .Range("F" & i).Value = htmlele.Children(5).textContent
    End With
    i = i + 1
    Next htmlele
End Sub

Thank you

【问题讨论】:

  • 我尝试检查 VBA 代码和 WSJ 网站的源代码。从 VBA 代码中,您似乎想要访问在具有类名称“zonedmodule”的第一个元素的 TR 标记的子项中后退的文本数据。如果我们使用开发人员工具参考源代码,那么您会注意到第一个具有类名“zonedmodule”的元素是空的。由于这个原因,您不会在 Excel 工作表中获得任何数据。如果您可以显示要从该站点访问的数据的快照,那么我们可以尝试为此提供一些代码示例。
  • 亲爱的迪帕克,非常感谢您的回复。我在我的原始帖子中添加了该数据的快照,我想导入。这是 GOL 航空公司的资产负债表。再次感谢您的帮助。

标签: excel vba internet-explorer


【解决方案1】:

以下示例代码可以帮助您获取资产的表格数据。

Option Explicit
Public Sub demo()
    Dim IE As New InternetExplorer, clipboard As Object
    With IE
        .Visible = True
        .navigate "https://your_site_address_here"  'please modify site address here...

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

        Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
     
            ThisWorkbook.Worksheets("Sheet1").Cells(1, 1) = "Assets"
            clipboard.SetText IE.document.getElementsByClassName("cr_dataTable")(0).outerHTML
            clipboard.PutInClipboard
       
        ThisWorkbook.Worksheets("Sheet1").Cells(2, 1).PasteSpecial
        .Quit
    End With
End Sub

注意:您只需在此示例代码中添加您的网站地址即可。

测试结果:

此外,您可以根据自己的要求修改此代码示例。如果您对此问题还有任何疑问,请告诉我们。

感谢您的理解。

【讨论】:

    猜你喜欢
    • 2019-11-07
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2017-06-19
    相关资源
    最近更新 更多