Does anyone on here know VBA? I need help understanding why a Macro I wrote in excel isn't working right. This program is supposed to take what is in Cells(2, 7), run a google search, collect the resultstats, and record those stats in Cells(2, 4). Then shift down one line and run again until the cell is empty. The problem is I run into error 80070005, which from what I've read is because google doesn't let you do a lot of searches at a time. To get around this, I'm trying to program the code to on error change the XMLHTTP object and continue on the list. However, I keep running into an overflow error on the ErrCount = ErrCount + 1. I'm not sure why or how to avoid it. I'm only just now learning VBA, so I could really use some help. Any thoughts?
here's the code:
here's the code:
Sub GoogleSearchExcelResults()
Dim url As String, lastRow As Long
Dim XMLHTTP As Object, html As Object
Dim start_time As Date
Dim end_time As Date
Dim Selec1 As Object
Dim Selec2 As Object
Dim rowNo As Integer
Dim ErrCount As Integer
rowNo = 2
ErrCount = 1
Set Selec1 = Cells(rowNo, 7)
Set Selec2 = Cells(rowNo, 4)
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
Do Until Selec1.Value = ""
On Error GoTo XMLErr
Set Selec1 = Cells(rowNo, 7)
Set Selec2 = Cells(rowNo, 4)
url = "https://www.google.co.in/search?q=" & Selec1 & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)
Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
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
If html.getElementById("resultStats") Is Nothing Then
str_text = "0 Results"
Else
str_text = html.getElementById("resultStats").innerText
End If
Selec2 = str_text
rowNo = rowNo + 1
XMLErr: If Err Then ErrCount = ErrCount + 1
If ErrCount = 1 Then Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
If ErrCount = 2 Then Set XMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
If ErrCount = 3 Then Set XMLHTTP = CreateObject("MSXML2.XMLHTTP.6.0")
If ErrCount = 4 Then Set XMLHTTP = CreateObject("MSXML2.SERVERXMLHTTP")
If ErrCount = 5 Then Set XMLHTTP = CreateObject("MSXML2.SERVERXMLHTTP.3.0")
If ErrCount = 6 Then Set XMLHTTP = CreateObject("MSXML2.SERVERXMLHTTP.6.0")
If ErrCount = 7 Then Set XMLHTTP = CreateObject("Microsoft.XMLHTTP")
If ErrCount = 8 Then Set XMLHTTP = CreateObject("Microsoft.XMLHTTP.1.0")
If ErrCount = 9 Then Exit Do
Loop
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