Search code examples
excelfor-looponerrorvba

On Error - Using loop that won't be interrupted irregardless of the error


I made a code that will use the cells in column A and search it in a website and pull down a certain table I need. However, sometimes I get an error (because the name is wrong or whatever), and I want the code to skip it and move on.

Example: if searched A3 was a problem, and I want the code to search A4, etc.

The Error is Run-time error '91': object variable or With block variable not set

Code:

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

Solution

  • This can raise an error:

    Set tblEle = objIE.document.getElementsByClassName("cTblListBody")(5) 
    

    For example, if no matching element with classname "cTblListBody" is found, or if there are fewer than 6 of those items found, etc. In this case, your error handler will take over, and the tblEle will be Nothing.

    This statement Resume Next will attempt to resume execution on the next line (from the line which raised the error), and these lines will of course fail if the tblEle has not been assigned (i.e., Is Nothing).

    Sheets("Sheet2").Range("B" & i).Value = tblEle.innerText
    Debug.Print tblEle.innerText
    

    You don't want to Resume Next, you want to resume at the next iteration of the loop, so you can do this with another label:

    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
    
    NextItem:
    Next
    
    ErrorHandler
    
    Resume NextItem
    

    Or, more appropriately (IMHO), trap this error:

    '## THIS LINE IS NOT NEEDED: 
    ' 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")
        If Not tblEle Is Nothing Then
            If tblEle.length > 5 Then
                Sheets("Sheet2").Range("B" & i).Value = tblEle(5).innerText
                Debug.Print tblEle(5).innerText
            End If
        End If
    Next
    
    '## THESE ARE NOT NEEDED:
    'ErrorHandler
    'Resume NextItem
    

    Note: this assumes that at least 6 cTblListBody are found, if that may not be the case, additional logic may be required.