Vba 无法将输出结果索引到excel中

Vba 无法将输出结果索引到excel中,vba,excel,Vba,Excel,我有一个从A2:A966到A2:A966的电影列表,我将浏览A2:A966中的电影列表,并将收视率数据拉入B2:966,将总评论拉入C2:C966 输出不会显示在单元格B2和C2中,但是运行宏不会产生错误。我尝试了各种各样的变化,甚至尝试让msgbox显示结果,但仍然一无所获 sub imd_data() Dim IE As New InternetExplorer, html As HTMLDocument, ele As Object With IE .Vis

我有一个从A2:A966到A2:A966的电影列表,我将浏览A2:A966中的电影列表,并将收视率数据拉入B2:966,将总评论拉入C2:C966

输出不会显示在单元格B2和C2中,但是运行宏不会产生错误。我尝试了各种各样的变化,甚至尝试让msgbox显示结果,但仍然一无所获

sub imd_data()


 Dim IE As New InternetExplorer, html As HTMLDocument, ele As Object

    With IE
        .Visible = True
        .navigate "http://www.google.com/"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set html = .document
        Set sht = Sheets("Sheet2")           

    End With

    html.getElementById("lst-ib").Value = sht.Range("A2") & " IMDB"
    html.getElementById("btnK").Click
    Application.Wait Now + TimeValue("00:00:05")
    html.getElementsByClassName("rc")(0).getElementsByTagName("a")(0).Click
    Application.Wait Now + TimeValue("00:00:05")

    For Each ele In html.getElementsByClassName("imdbRating")
        With ele.getElementsByClassName("ratingValue")
            If .Length Then r = r + 1: Cells(r, 1) = .Item(0).innerText
            MsgBox .Item(0).innerText
            sht.Range("B2") = .Item(0).innerText
        End With
        With ele.getElementsByClassName("small")
            If .Length Then Cells(r, 2) = .Item(0).innerText
             sht.Range("C2") = .Item(0).innerText
        End With
    Next ele    
    IE.Quit    

End Sub

试着在代码的每一部分中引用
sht
。例如,重写这个:

For Each ele In html.getElementsByClassName("imdbRating")
    With ele.getElementsByClassName("ratingValue")
        If .Length Then r = r + 1: sht.Cells(r, 1) = .Item(0).innerText
        MsgBox .Item(0).innerText
        sht.Range("B2") = .Item(0).innerText
    End With
    With ele.getElementsByClassName("small")
        If .Length Then sht.Cells(r, 2) = .Item(0).innerText
         sht.Range("C2") = .Item(0).innerText
    End With
Next ele
Sub ImdbData()    
    Dim IE As New InternetExplorer, html As HTMLDocument, ele As Variant
    Dim sht     As Worksheet
    Dim r       As Long        
    With IE
        .Visible = True
        .navigate "http://www.google.com/"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set html = .document
    End With

    Set sht = Worksheets(1)

    html.getElementById("lst-ib").value = "The incredibles " & " IMDB"
    html.getElementById("btnK").Click
    Application.Wait Now + TimeValue("00:00:05")
    html.getElementsByClassName("rc")(0).getElementsByTagName("a")(0).Click
    Application.Wait Now + TimeValue("00:00:05")

    For Each ele In html.getElementsByClassName("imdbRating")
        With ele.getElementsByClassName("ratingValue")
            If .Length Then r = r + 1: Cells(r, 1) = .Item(0).innerText
            MsgBox .Item(0).innerText
            sht.Range("B2") = .Item(0).innerText
        End With
        With ele.getElementsByClassName("small")
            If .Length Then Cells(r, 2) = .Item(0).innerText
             sht.Range("C2") = .Item(0).innerText
        End With
    Next ele
    IE.Quit

End Sub
因此,添加
sht.Cells
两次。在不引用单元格的父工作表的情况下,来自
范围(“C2”)
单元格(r,2)
的值取自
活动工作表
。判断您的父工作表应该是
Sheet2
,那么很可能这不是您的
ActiveSheet

只是一个想法-不要在
中使用
,如果
,则很难遵循,而且它并不总是你(或至少我)所期望的-


对我有效的代码是:

For Each ele In html.getElementsByClassName("imdbRating")
    With ele.getElementsByClassName("ratingValue")
        If .Length Then r = r + 1: sht.Cells(r, 1) = .Item(0).innerText
        MsgBox .Item(0).innerText
        sht.Range("B2") = .Item(0).innerText
    End With
    With ele.getElementsByClassName("small")
        If .Length Then sht.Cells(r, 2) = .Item(0).innerText
         sht.Range("C2") = .Item(0).innerText
    End With
Next ele
Sub ImdbData()    
    Dim IE As New InternetExplorer, html As HTMLDocument, ele As Variant
    Dim sht     As Worksheet
    Dim r       As Long        
    With IE
        .Visible = True
        .navigate "http://www.google.com/"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set html = .document
    End With

    Set sht = Worksheets(1)

    html.getElementById("lst-ib").value = "The incredibles " & " IMDB"
    html.getElementById("btnK").Click
    Application.Wait Now + TimeValue("00:00:05")
    html.getElementsByClassName("rc")(0).getElementsByTagName("a")(0).Click
    Application.Wait Now + TimeValue("00:00:05")

    For Each ele In html.getElementsByClassName("imdbRating")
        With ele.getElementsByClassName("ratingValue")
            If .Length Then r = r + 1: Cells(r, 1) = .Item(0).innerText
            MsgBox .Item(0).innerText
            sht.Range("B2") = .Item(0).innerText
        End With
        With ele.getElementsByClassName("small")
            If .Length Then Cells(r, 2) = .Item(0).innerText
             sht.Range("C2") = .Item(0).innerText
        End With
    Next ele
    IE.Quit

End Sub
我只是简单地添加了
“不可信的”
,而不是
sht.Range(“A2”)
,因此我确信它将加载一部电影,并在imdb.com中找到
ElementByClassName(“imdbRating”)

此外,当您在StackOverflow中询问时,最好提及用于早期绑定的库。因此,您可以为每个人节省大约3-4分钟的搜索时间。在您的情况下,您正在使用:

  • Microsoft Internet控件
  • Microsoft HTML对象库
可以跳过标准选项:


或者简单地使用后期绑定。最后,如果你考虑互联网爬行,它可以,但如果这不是你的情况,也许用Beautiful Soup和Python更好。

我有点不知道如何编辑它,代码反映sheet2是activesheet,我是否删除了代码中的单元格部分?@JohnGendi-将我的代码与您的代码部分进行比较-。我只是简单地添加了两次
sht
。即使进行了此修改,我仍然没有得到任何结果output@JohnGendi-那么您是否在
MsgBox()
上获得了所需的信息?否,甚至没有出现MsgBox。这是否意味着我可能错误地使用了内部文本?