对象变量或块变量未设置vba

时间:2015-12-09 17:24:10

标签: excel vba excel-vba

所以我尝试使用此代码从网站上抓取数据。它从sheet1中提取数据,然后导航并搜索人员。然后它获得某些html标签innertext。这部分是工作时间。有时所有5个页面都已成功打开,但是在sheet2中缺少数据,我想要分配搜索值,有时它只在导航2页时就会给出上面的错误。 VBA告诉我错误是符合的: PhoneNumber(i)= Doc.getElementsByTagName(" td")(2).innerText 如何在没有错误的情况下成功获取所有必需的信息。

Sub PeopleFinders()

Dim i As Integer
Dim ie As Object
Dim firstname(1 To 5), lastname(1 To 5) As Variant
Dim mm(1 To 5), dd(1 To 5), yyyy(1 To 5) As Variant
Dim PhoneNumber(1 To 5) As Variant
Dim Address(1 To 5) As Variant

For i = 1 To 5
    firstname(i) = Sheet1.Cells(i, 1).Value
    lastname(i) = Sheet1.Cells(i, 2).Value
    mm(i) = Sheet1.Cells(i, 3).Value
    dd(i) = Sheet1.Cells(i, 4).Value
    yyyy(i) = Sheet1.Cells(i, 5).Value

    Set ie = CreateObject("InternetExplorer.Application")

    ie.Visible = True
    ie.navigate ("http://www.peoplefinders.com/peoplesearch/searchresults?search=People&fn=" & firstname(i) & "&mn=&ln=" & lastname(i) & "&city=&state=&age=&dobmm=" & mm(i) & "&dobdd=" & dd(i) & "&doby=" & yyyy(i))

    Do While ie.Busy: DoEvents: Loop
    Dim Doc As HTMLDocument
    Set Doc = ie.document

    PhoneNumber(i) = Doc.getElementsByTagName("td")(2).innerText
    Address(i) = Doc.getElementsByTagName("td")(1).innerText

    Sheet5.Cells(i, 1).Value = PhoneNumber(i)
    Sheet5.Cells(i, 2).Value = Address(i)

Next

Set ie = Nothing

End Sub

0 个答案:

没有答案