VBA Giving Run-time error '91'

brandonscott2202

New Member
Joined
Mar 7, 2018
Messages
2
I'm using the following to return links from a search on Google:

Sub XMLHTTP()


Dim url As String, lastRow As Long
Dim XMLHTTP As Object, html As Object, objResultDiv As Object, objH3 As Object, link As Object
Dim start_time As Date
Dim end_time As Date


lastRow = Range("A" & Rows.Count).End(xlUp).Row


Dim cookie As String
Dim result_cookie As String


start_time = Time
Debug.Print "start_time:" & start_time

For i = 2 To lastRow

url = "https://www.google.co.in/search?q=" & Cells(i, 1) & "+" & Cells(i, 7) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)


Set XMLHTTP = CreateObject("MSXML2.serverXMLHTTP")
XMLHTTP.Open "GET", url, False
XMLHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
XMLHTTP.send
Set html = CreateObject("htmlfile")
html.body.innerHTML = XMLHTTP.ResponseText
Set objResultDiv = html.getelementbyid("rso")
Set objH3 = objResultDiv.getelementsbytagname("H3")(0)
Set link = objH3.getelementsbytagname("a")(0)




str_text = Replace(link.innerHTML, "", "")
str_text = Replace(str_text, "
", "")


Cells(i, 8) = str_text
Cells(i, 9) = link.href
DoEvents
Next


end_time = Time
Debug.Print "end_time:" & end_time


Debug.Print "done" & "Time taken : " & DateDiff("n", start_time, end_time)
MsgBox "done" & "Time taken : " & DateDiff("n", start_time, end_time)
End Sub



After over 800 lines returned, it threw the Error '91'

Any suggestions?

EDIT:
Specifically the Error '91' is on line:
Set objH3 = objResultDiv.getelementsbytagname("H3")(0)
 
Last edited by a moderator:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Is it possible the randbetween repeated a number and caused an issue with regard to having 2 objects the same, the randbetween does not guarantee to produce unique numbers
 
Last edited:
Upvote 0
What's the error message?
 
Upvote 0
That sounds to me that no elements with the tagname 'H3' have been found, and if that's the case then obviously you can return the first item in the collection.

To avoid the error you could use something like this.
Code:
Option Explicit

Sub XMLHTTP()
Dim url As String, LastRow As Long
Dim XMLHTTP As Object, html As Object, objResultDiv As Object, objH3 As Object, link As Object
Dim start_time As Date
Dim end_time As Date
Dim cookie As String
Dim result_cookie As String
Dim str_text As String
Dim I As Long

    LastRow = Range("A" & Rows.Count).End(xlUp).Row

    start_time = Time
    
    Debug.Print "start_time:" & start_time

    For I = 2 To LastRow

        url = "https://www.google.co.in/search?q=" & Cells(I, 1) & "+" & Cells(I, 7) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)


        Set XMLHTTP = CreateObject("MSXML2.serverXMLHTTP")
        XMLHTTP.Open "GET", url, False
        XMLHTTP.setRequestHeader "Content-Type", "text/xml"
        XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
        XMLHTTP.send

        Set html = CreateObject("htmlfile")
        html.body.innerHTML = XMLHTTP.ResponseText

        Set objResultDiv = html.getelementbyid("rso")

        Set objH3 = objResultDiv.getelementsbytagname("H3")

        If Not objH3 Is Nothing Then
            Set objH3 = objH3(0)
            Set link = objH3.getelementsbytagname("a")(0)


            str_text = Replace(link.innerHTML, "", "")
            str_text = Replace(str_text, "", "")


            Cells(I, 8) = str_text
            Cells(I, 9) = link.href
        Else
            Cells(I, 8).Resize(, 2).Value = "N/A#"
        End If

        DoEvents

    Next

    end_time = Time
    Debug.Print "end_time:" & end_time


    Debug.Print "done" & "Time taken : " & DateDiff("n", start_time, end_time)
    MsgBox "done" & "Time taken : " & DateDiff("n", start_time, end_time)

End Sub
Note, this will only avoid the error.

If you want to find out why you are getting the error try checking the url when the error occurs.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top