VBA code to retrieve Google Results!

neodjandre

Well-known Member
Joined
Nov 29, 2006
Messages
950
Office Version
  1. 2019
Platform
  1. Windows
Hello,

I have a name in cell "C1".

Can someone genius enough think of VBA code to enter the cell contents of "C1" in a google search and return the website address of the first google result in cell "D1" ?

many thanks guys!
andy
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I got this from some place...maybe here. I can't recall.

Code:
Sub AutomateIE() 
    Dim ie As InternetExplorer 
    Dim RegEx As RegExp, RegMatch As MatchCollection 
    Dim MyStr As String 
     
    Set ie = New InternetExplorer 
    Set RegEx = New RegExp 
     
     'Search google for cell value
    ie.Navigate "http://www.google.com.au/searchhl=en&q=antsmarching.org&meta=" 
     
     'Loop unitl ie page is fully loaded
    Do Until ie.ReadyState = READYSTATE_COMPLETE 
    Loop 
     
     'String to parse google search for an ants reference
    With RegEx 
        .Pattern = sheets(1).range(C1).value 
        .MultiLine = True 
    End With 
     
     'return text from google page
    MyStr = ie.Document.body.innertext 
    Set RegMatch = RegEx.Execute(MyStr) 
     
     'If a match to our RegExp searchstring is found then launch this page
    If RegMatch.Count > 0 Then 
        ie.Navigate RegMatch(0) 
        Do Until ie.ReadyState = READYSTATE_COMPLETE 
        Loop 
        MsgBox "Loaded Google link" 
         'show internet explorer
        ie.Visible = True 
    Else 
        MsgBox "No link found" 
    End If 
     
    Set RegEx = Nothing 
    Set ie = Nothing 
End Sub

Should get you started.

HTH,
Roger
 
Upvote 0
thanks for the help.

I am getting object not defined at this line

Code:
Dim ie As InternetExplorer

do you know why? do I need to change VBA references in the VBA editor ?
 
Upvote 0
ok I have set up the correct references but the code is not really working and is actually not applied to my specific scenario... any more ideas?
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top