Private Sub CommandButton1_Click()
' **** UserForm1 Input Data in to Textboxs*****
Worksheets("Keywords").Range("C3") = TextBox1.Text ' keyword
Worksheets("Keywords").Range("C4") = TextBox2.Text ' pages to search
Worksheets("Keywords").Range("C5") = TextBox3.Text 'time delay 1
Worksheets("Keywords").Range("C6") = TextBox4.Text 'time delay 2
'**** Keyword URL SCRAPER *****
Dim ie As Object
Dim HTMLdoc As Object
Dim nextPageElement As Object
Dim div As Object
Dim link As Object
Dim url As String
Dim pageNumber As Long
Dim i As Long
' ***** Takes seach Criteria from Sheet2 caleld "Keyword" Cell C3 and places it into google *****
url = "https://www.google.co.uk/search?q=" & Replace(Worksheets("Keywords").Range("C3").Value, " ", "+")
' ***** Gets internet explorer read, which is set to False so does NOT SHOW ******
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = False
.navigate url
Do While .Busy Or .readyState <> 4
DoEvents
Loop
End With
Application.Wait Now + TimeSerial(0, 0, 5)
Set HTMLdoc = ie.document
'***** Searches for URLs and places the results into Sheet1 caleld "DATA" ROW 2 Column A *****
With Sheets("DATA")
pageNumber = 1
i = 2
Do
For Each div In HTMLdoc.getElementsByTagName("div")
If div.getAttribute("class") = "r" Then
Set link = div.getElementsByTagName("a")(0)
.Cells(i, 1).Value = link.getAttribute("href")
i = i + 1
End If
Next div
'****** Searches Number of Pages entered in Keyword Sheet Cell C4, E.G 5pages *******
If pageNumber >= Replace(Worksheets("Keywords").Range("C4").Value, " ", "+") Then Exit Do
On Error Resume Next
Set nextPageElement = HTMLdoc.getElementById("pnnext")
If nextPageElement Is Nothing Then Exit Do
'****** Scrolls Down the Browser to mimic human behaviour *****
ie.document.parentWindow.Scroll 0&, 99999
'***** 1st Random delay in seconds from Max number entered in "Keyword" sheet C5 *****
Application.Wait Now + TimeSerial(0, 0, Application.RandBetween(1, Worksheets("Keywords").Range("C5").Value))
'***** clicks on google next page ******
nextPageElement.Click
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
'***** 2nr Random delay from Max number entered in "Keyword" sheet C6 *****
Application.Wait Now + TimeSerial(0, 0, Application.RandBetween(1, Worksheets("Keywords").Range("C6").Value))
Set HTMLdoc = ie.document
pageNumber = pageNumber + 1
Loop
End With
ie.Quit
Set ie = Nothing
Set HTMLdoc = Nothing
Set nextPageElement = Nothing
Set div = Nothing
Set link = Nothing
MsgBox "All Done"
End Sub