【问题标题】:VBA Excel 2010 Run-time error'91' Object variable or With Block variable not setVBA Excel 2010 运行时错误“91”对象变量或未设置块变量
【发布时间】:2016-11-12 01:25:20
【问题描述】:

我尝试了许多教程,并在堆栈溢出上花费了大量时间来尝试解决此问题,但始终找不到答案。

我是使用 excel 的 VBA 新手,一直在尝试在 excel 中进行自动网络搜索,该搜索从一列单元格中获取查询并将结果中的一个元素写入另一行单元格。为了使这个示例简单,我使用了 Google 搜索。

我总是得到相同的信息:

运行时错误'91'对象变量或未设置块变量

error screenshot

这是最新版本的代码:

Sub Macro1()

 Dim ie As Object
 Set Rng = Range("A3:A5")

Set Row = Range(Rng.Offset(1, 0), Rng.Offset(1, 0).End(xlDown))

Set ie = CreateObject("InternetExplorer.Application")
With ie
    .Visible = True
    For Each Row In Rng
    .navigate "https://www.google.com/#q=" & Range("A" & Row.Row).Value

Do
    DoEvents
        Loop Until ie.readyState = READYSTATE_COMPLETE

        Dim doc As HTMLDocument
        Set doc = ie.document
        While ie.readyState <> 4

Wend

Range("B" & Row.Row) = doc.getElementById("resultStats" & Range("A" & Row.Row).Value).innerText

Next Row

ie.Quit

End With

End Sub

非常感谢任何帮助。

谢谢,

【问题讨论】:

  • 看起来像 doc.getElementById("resultStats" &amp; Range("A" &amp; Row.Row).Value)Range("A" &amp; Row.Row) 可能评估为 Null。您能否确认Range("A" &amp; Row.Row) 实际上返回了一个非空值,并且您的 HTML 文档中有一个元素实际上名为“resultStatsA”?
  • 如何使用适当的缩进,这样您就可以看到哪些代码行打开了一个块,哪些关闭了它?这将使您的代码更易于阅读和排除故障。

标签: excel getelementbyid vba


【解决方案1】:

它不起作用的原因是因为在 Google 上没有名为“resultStatsYOURVALUEHERE”的元素。 getElementById("resultStats") 将返回结果的数量,例如如果您在 Google 上搜索 VBA,则会获得 36,100,000 个结果。
此外,您应该使用Option Explicit

如果你想返回“Header”和“URL”,你可以这样做。但是,A3 中只能有 1 个值 - 当然可以修改。

Option Explicit

Sub Macro1()
Dim rng As Range, Cell As Range
Dim i As Integer
Dim IE As Object, objDiv As Object, objH3 As Object, objLink As Object
Dim strText As String

Set rng = Range("A3:A5")

Set Cell = Range(rng.Offset(1, 0), rng.Offset(1, 0).End(xlDown))

Set IE = CreateObject("InternetExplorer.Application")

    With IE
        .Visible = False ' Change to True if you want to open IE and have it visible

        For Each Cell In rng
        .navigate "https://www.google.com/#q=" & Range("A" & Cell.row).value

        Do
            DoEvents
                Loop Until IE.readyState = READYSTATE_COMPLETE

                Dim doc As HTMLDocument
                Set doc = IE.document
                While IE.readyState <> 4
        Wend

            ' Add the 5 first results to rows
            For i = 0 To 4
                Set objDiv = doc.getElementById("rso")
                Set objH3 = objDiv.getElementsByTagName("H3")(i)
                Set objLink = objH3.getElementsByTagName("a")(0)

                strText = Replace(objLink.innerHTML, "<EM>", "")
                strText = Replace(strText, "</EM>", "")

                Dim CellRow As Integer
                If i = 0 Then
                    CellRow = Cell.row
                Else
                    CellRow = CellRow + 1
                End If

                ' Insert values starting in B3 and C3 and continue with B4, C4, for the next value etc.
                Cells(CellRow, 2) = strText
                Cells(CellRow, 3) = objLink.href
            Next i

        Next Cell

    IE.Quit

    End With
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多