I have been using below code and it is giving me an error that is Run Time Error '91' : Object Variable or With Block not Set
I do not know why.
Earlier it was working fine but i do not know why an error is occur. I have tried other solutions searching Copy URL from the first Search
but they also were not working.
error appear on this line Set link = ecoll.getElementsByTagName("a")(0)
If someone can provide alternate solution it will be great help. Any help will be appreciated.
Sub link()
Dim doc As HTMLDocument
Set doc = New HTMLDocument
Dim lastrow As Long
Dim ecoll As Object
Dim link As Object
Dim t As Date
lastrow = Range("A" & Rows.Count).End(xlUp).Row
t = Now()
Dim reqObj As Object
Set reqObj = CreateObject("MSXML2.XMLHTTP")
For i = 2 To lastrow
reqObj.Open "GET", "https://www.google.co.in/search?q=" & Cells(i, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000), False
reqObj.send
doc.body.innerHTML = reqObj.responseText
Set ecoll = doc.getElementById("rso")
Set link = ecoll.getElementsByTagName("a")(0)
Cells(i, 2) = link.href
Next
Set doc = Nothing
Set reqObj = Nothing
Debug.Print "done" & "Time taken : " & Format(Now() - t, "hh:mm:ss")
MsgBox "Ellapsed Time - " & Format(Now() - t, "hh:mm:ss")
End Sub