我做了一個代碼,它將使用A列中的單元格並在網站中搜索並拉下我需要的某個表。但是,有時候我得到一個錯誤(因爲名稱是錯誤的或其他),我希望代碼跳過它並繼續前進。On Error - 使用不會中斷的循環,不管是否出現錯誤
示例:如果搜索的A3是一個問題,並且我想要的代碼搜索A4等
錯誤是運行時錯誤「91」:對象變量或帶塊變量未設置
代碼:
Sub SearchBot()
Dim objIE As InternetExplorer
Dim aEle As HTMLLinkElement
Dim y As Integer
Dim result As String
Dim tbl As Object, obj_tbl As Object
Dim lastRow As Long
Dim start_time As Date
Dim end_time As Date
Dim cookie As String
Dim result_cookie As String
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://website.com"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
For i = 2 To 1829
objIE.document.getElementById("SearchTopBar").Value = _
Sheets("Sheet2").Range("A" & i).Value '
Set oNode = objIE.document.getElementsByClassName("iPadHack tmbsearchright"
(0)
oNode.Click
On Error GoTo ErrorHandler
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Dim tblEle As Object
Set tblEle = objIE.document.getElementsByClassName("cTblListBody")(5)
Sheets("Sheet2").Range("B" & i).Value = tblEle.innerText
Debug.Print tblEle.innerText
Next
ErrorHandler
Resume Next
objIE.Quit
End Sub
當您逐句通過代碼時,它是否實際觸及ErrorHandler? –
它不是。它不會讓我啓動代碼說:編譯錯誤:子或功能未定義 – Sameer
我懷疑它可能是使用IE瀏覽器打開的參考。 第3個點的點https://msdn.microsoft.com/en-us/library/aa264506%28v=vs.60%29.aspx?f=255&MSPPError=-2147217396 –