【问题标题】:excel vba (internet explorer) error 424 when for loop exitsfor循环退出时的excel vba(Internet Explorer)错误424
【发布时间】:2016-11-21 17:00:49
【问题描述】:

从网站收集数据时,我在 Excel VBA 中的 For 循环结束时收到此错误。之前的所有代码都运行良好。

即使下面的代码也给了我结果,但它只是给了我一个错误。

y = 5
total = html.getElementsByTagName("table")(3).Children(0).Children.Length

For i = 0 To total
    Worksheets("FS Summary").Range("A" & y).Value = html.getElementsByTagName("table")(3).Children(0).Children(i).Children(0).Children(0).innerText
    Worksheets("FS Summary").Range("B" & y).Value = html.getElementsByTagName("table")(3).Children(0).Children(i).Children(1).Children(0).innerText
    Worksheets("FS Summary").Range("C" & y).Value = html.getElementsByTagName("table")(3).Children(0).Children(i).Children(2).Children(0).innerText
    Worksheets("FS Summary").Range("D" & y).Value = html.getElementsByTagName("table")(3).Children(0).Children(i).Children(3).Children(0).innerText
    Worksheets("FS Summary").Range("E" & y).Value = html.getElementsByTagName("table")(3).Children(0).Children(i).Children(4).Children(0).innerText
    Worksheets("FS Summary").Range("F" & y).Value = html.getElementsByTagName("table")(3).Children(0).Children(i).Children(5).Children(0).innerText
    Worksheets("FS Summary").Range("G" & y).Value = html.getElementsByTagName("table")(3).Children(0).Children(i).Children(6).Children(0).innerText
    Worksheets("FS Summary").Range("H" & y).Value = html.getElementsByTagName("table")(3).Children(0).Children(i).Children(7).Children(0).innerText
    y = y + 1
Next i

请告诉我如何解决它。

【问题讨论】:

  • 错误发生在哪一行?
  • 注意到这些重复的链式调用了吗?尝试取消链接它们(以便您可以检查运行时值)并提取一个函数以返回您正在寻找的InnerTexthandle runtime errors 在那个函数中;我认为您对 Children 的假设可能是错误的。
  • @iDevlop 如果我首次亮相并破坏代码,则错误会在循环存在后立即出现。我得到了完整的结果,但无法避免或识别错误。
  • foo().bar().baz().something().somethingElse() -> “链式调用”。我的意思是你可以做Set fooResult = foo 然后Set barResult = fooResult.bar() 然后Set bazResult = barResult.baz() 等等.. 也就是“解链”调用,看看哪个调用失败了。但请继续阅读。您需要弄清楚究竟是哪一行引发了错误。错误处理将帮助您做到这一点。
  • 亲爱的朋友们。感谢您在这个网站上给我一个愉快的体验。 @Tim Williams 给了我解决方案,并从 Mats Mug 中学到了新东西。

标签: vba excel web-scraping


【解决方案1】:

如果(例如)Children.Length 是 5,那么您的 i 循环应该从 0 变为 4(即 total - 1),因为没有 Children(5)

您的代码也将受益于一些简单的重构:

Dim y, ch, Total, i

y = 5
Set ch = html.getElementsByTagName("table")(3).Children(0).Children
Total = ch.Length

For i = 0 To Total-1
    With Worksheets("FS Summary")
        .Range("A" & y).Value = ch(i).Children(0).Children(0).innerText
        .Range("B" & y).Value = ch(i).Children(1).Children(0).innerText
        .Range("C" & y).Value = ch(i).Children(2).Children(0).innerText
        .Range("D" & y).Value = ch(i).Children(3).Children(0).innerText
        .Range("E" & y).Value = ch(i).Children(4).Children(0).innerText
        .Range("F" & y).Value = ch(i).Children(5).Children(0).innerText
        .Range("G" & y).Value = ch(i).Children(6).Children(0).innerText
        .Range("H" & y).Value = ch(i).Children(7).Children(0).innerText
    End With
    y = y + 1
Next i

【讨论】:

  • 我已经改变了总数 - 1 这实际上是有道理的。但仍然需要错误 424 对象。
  • 哪一行
  • 嗨蒂姆。实际上,总数 - 1 是错误。因为循环又运行了一次并且没有要返回的对象,这就是我收到该错误的原因。谢谢蒂姆。最后它起作用了。谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多