-2

Below is the error I'm getting when entering the codeI'm new to VBA but I'd like to create a macro that returns the top 5 search results of the item listed in cell A15 using either google chrome or microsoft edge? I tried adding in the code below and got an error.

1 Answers1

0

VBA can only automate the Internet Explorer browser. So if you want to use google chrome or Microsoft edge then this approach will not work for you.

Below is the sample code that populates Google search results to a worksheet.

Option Explicit
Public Sub GetLink()
    Dim ie As New InternetExplorer

    Dim url As String
    url = "https://google.co.uk/search?q=" + Sheet1.Range("A2").Value
    With ie
        .Visible = True
        .navigate url

        While .Busy Or .readyState < 4: DoEvents: Wend

        Sheet1.Range("B2").Value = .document.querySelector("#search div.r [href*=http]").href
        Sheet1.Range("C2").Value = .document.querySelector("#search div.r [href*=http]").innerText
        .Quit
    End With

End Sub

Output:

enter image description here

Reference:

How to get the first search result link of a google search using VBA?

Below is another helpful thread. Its solution uses the XMLHTTP object.

Using VBA in Excel to Google Search in IE and return the hyperlink of the first result

Further, you can try to check the above example and try to modify the sample as per your own requirements.

Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19
  • Hi- thanks for the update. I tried entering the code and got an error. I dropped a screenshot of the error into my original question above. The error was compile error - user defined type not defined. – Lyoung1211 Jun 16 '20 at 13:57
  • Please go to Tools-> References. Checked the references for 'Microsoft Internet Controls' and 'Microsoft HTML Object Library'. Then after again run the code. See here: https://imgur.com/a/mieHopR – Deepak-MSFT Jun 17 '20 at 05:31