【问题标题】:Run-time error '91': Object variable or With block variable not set运行时错误“91”:对象变量或未设置块变量
【发布时间】:2016-10-07 05:37:23
【问题描述】:
Sub amazon()

Dim x As Long
Dim y As Long
Dim doc As HTMLDocument
Dim htmTable As HTMLTable
Dim Elements As IHTMLElementCollection
Dim Element As IHTMLElement
Dim bf As String
Dim str As String
Dim af As String
Dim title As String
Dim prc()
Dim SrtTemp As Variant
Dim i As Long, q As Long
Dim j As Long
Dim n As Long
  n = Sheets("temp").Cells(Rows.Count, 5).End(xlUp).Row
For q = 7 To n
    Sheets("temp").Cells(q, 5) = ""
   Sheets("temp").Cells(q, 6) = ""
Next q
str = ""
   str = Sheets("temp").Cells(1, 4)
   With CreateObject("MSXML2.XMLHTTP")
     .Open "GET", "http://www.amazon.in/s/ref=sr_nr_n_0?fst=as%3Aoff&rh=n%3A976419031%2Cn%3A1805560031%2Ck%3A" & str & "&keywords=" & str & "&ie=UTF8&qid=1437023564&rnid=976420031", ""
     .send

     Do: DoEvents: Loop Until .readyState = 4
     Set doc = New MSHTML.HTMLDocument
     doc.body.innerHTML = .responseText

       Set Elements = doc.getElementsByClassName("s-item-container")
       x = Elements.Length
      '' Debug.Print x
       ReDim prc(0 To x)
       z = 7
       y = 0
       For Each Element In Elements
           title = Element.Children(1).innerText
          '' Debug.Print title
           If InStr(UCase(title), UCase(str)) Then
                    Sheets("temp").Cells(z, 5) = title
                    Price = Element.Children(2).Children(0).Children(0).innerText
                    If InStr(UCase(Price), UCase("offer")) = 0 Then

                        bf = Price
                       '' Debug.Print bf
                        prc(y) = Trim(CDbl(bf))
                        On Error Resume Next
                      ''  Debug.Print prc(y)
                        Sheets("temp").Cells(z, 6) = prc(y)
                    Else
             ''    bf = Element.Children(2).Children(3).Children(0).Children(3).innerText
                        bf = Price
                      ''  Debug.Print bf

                        prc(y) = Trim(CDbl(Right(bf, 9)))
                        Sheets("temp").Cells(z, 6) = prc(y)

                      ''  Debug.Print prc(y)
                    End If
                    y = y + 1
                    z = z + 1
           End If
        Next Element
        Set Elements = Nothing

        .abort
        On Error Resume Next
    End With

End Sub

当我调试时,我收到错误 Run-time error '91': Object variable or With block variable not set at this line title = Element.Children(1).innerText 请帮我解决这个问题。

我在 Excel 工作表中使用 amazon 作为宏。我使用的是 excel 2016,当我调试此宏时出现此错误。所以请提供解决此问题的解决方案。

如果我使用 Set title = Element.Children(1).innerText 那么我会收到 Object required 错误。

我是 Vba 的新手,所以请帮我解决这个问题。

【问题讨论】:

    标签: excel excel-2016 vba


    【解决方案1】:

    试试:

    if not Element is nothing then
        if not Element.Children(1) is nothing then
            title = Element.Children(1).innerText
        end if
    end if
    

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    猜你喜欢
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多