1

Hi I'm trying to get VBA to run a script to bring back all the links from the first page after running a google search, but it doesn't consistently search all the cells in the column.

Also can anyone help me get the hyperlinks and innertext separate? Here's a copy of the code below:

'Start the bot called SearchBot
Sub SearchBot()

'declare/set aside memory for our variables
Dim wb As Workbook
Dim ws As Worksheet


Set wb = ActiveWorkbook
Set ws = ActiveSheet

For h = 1 To ws.Range("A1").CurrentRegion.Rows.Count
    Dim objIE As Object
    Dim aEIe As HTMLLinkElement
    Dim y As Integer
    Dim result As String

    'Start Internet Explorer
    Set objIE = New InternetExplorer

    'Make Internet Explorer Visible
    objIE.Visible = True

    'navigate to the google webpage
    objIE.navigate "google.com"

    'Wait for a few seconds while the browser is busy
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    'in the search box enter the field from the cell and press search
    objIE.document.getElementsByName("q")(0).Value = ws.Cells(h, 1).Value

    SendKeys "{Enter}"

    'Wait again for the browser to finish

    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    ws.Cells(h, 2).Select

    x = 0

    For Each aEIe In objIE.document.getElementsByClassName("r")


        ActiveCell = objIE.document.getElementsByClassName("r")(x).innerText
        ActiveCell.Offset(0, 1).Select
        x = x + 1
    Next

    objIE.Quit

Next



End Sub
jamheadart
  • 5,047
  • 4
  • 32
  • 63
T17
  • 71
  • 7
  • Just a piece of advice - you will be much better off working with background http requests rather than using internet explorer instances: https://stackoverflow.com/questions/158633/how-can-i-send-an-http-post-request-to-a-server-from-excel-using-vba – jamheadart May 28 '20 at 12:17

0 Answers0