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

时间:2017-12-13 16:55:45

标签: vba excel-vba excel

我有一张电影列表,其中包含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

1 个答案:

答案 0 :(得分:1)

尝试在代码的每个部分引用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

因此,添加sht.Cells两次。如果您没有引用单元格的父表单,则Range("C2")Cells(r,2)中的值取自ActiveSheet。判断您的父表格应为Sheet2,那么很可能这不是您的ActiveSheet

只是一个想法 - 不要在:中使用If,这很难遵循,并不总是你(或至少我)期望的那样 - {{3} }

对我有用的代码就是这个:

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

我只是添加了"The incredibles "而不是sht.Range("A2"),因此我确信它会加载一部电影,它会在imdb.com中找到ElementByClassName("imdbRating")

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

  • Microsoft Internet Controls
  • Microsoft HTML对象库

可以跳过标准的:

VBA - How the colon `:` works in VBA code with condition

或者只是使用后期绑定。最后,如果你考虑互联网爬行,它可以enter image description here,但如果不是你的情况,可能最好用Beautiful Soup和Python来做。