Perform Google Search on Internet Explorer using Excel VBA | Excel VBA

Yodalearning>Tutorials>Perform Google Search on Internet Explorer using Excel VBA | Excel VBA

First time in my life I have come across to create this type of automation and finally, I got success.

Yes-Google-Search-in-Excel

How to search data in Google using Excel VBA:

For example:

  • Write “Apple” in Google search box
  • Press Enter↵
    It will show you the statistics that how many records it has populated and also how much time it has taken to fetch the records.

Picture-1 Google Search

(Picture 1) Google search using Excel VBA

We will do the same using Excel VBA. Isn’t it interesting? I also didn’t have any idea about it. But by googling I got the idea and learned that you can do almost anything with excel VBA & wanted to share it with you.

If you open any page in IE (Internet Explorer) and you press F12 then you will get the HTML of that page. All you need to do is you will have to take the id of the specific box.

First Open Google in IE and then press F12. You will move to another HTML page. The page will look like below

Picture-2 HTML Page

(Picture 2) Google search using Excel VBA

Now Press on the arrow (Red circle in Picture 2). And you are forced to go back to Google page. Then place your on Google search box and then click on the box. As soon as you click on the box again you will be in HTML page and this time, you will get everything of that box like name, title, id etc…

Picture-3 HTML Box

(Picture 3) google search using Excel VBA

Now we need to use the id (“lst-ib”) in our code. And in this way, you will get the id of any DOM object. Sometimes “id” is not available and then we need to use a name in our code.

Now you will come to our excel file. We have some inputs in our excel file. All we need to do is we need to open Google and will have to fetch the statistics of the search result. Our input in excel file is like below picture

Picture-4 Input in Excel File

(Picture 4) google search using Excel VBA

Now we will take one by one keyword and search the keyword in Google and after opening the other page we will fetch the result.

Please follow the below steps to get the result:

  1. Open VBA page by pressing ALT + F11
  2. Go to Insert and then Module
  3. Copy the below code and paste in the Module
  4. Go to Tools and then Reference and select all the reference as shown in Picture 5
  5. Run the code by pressing F5 or from Run button

Picture-5 Refrence Box

(Picture 5) google search using Excel VBA

Here is the code 

Sub SearchGoogle()

Dim ie As Object

Dim form As Variant

Dim button As Variant

Dim LR As Integer

Dim var As String

Dim var1 As Object

LR = Cells(Rows.Count, 1).End(xlUp).Row

For x = 2 To LR

var = Cells(x, 1).Value

Set ie = CreateObject("internetexplorer.application")

ie.Visible = True

With ie

.Visible = True

.navigate "http://www.google.co.in"

While Not .readyState = READYSTATE_COMPLETE

Wend

End With

'Wait some to time for loading the page

While ie.Busy

DoEvents

Wend

Application.Wait (Now + TimeValue("0:00:02"))

ie.document.getElementById("lst-ib").Value = var

'Here we are clicking on search Button

Set form = ie.document.getElementsByTagName("form")

Application.Wait (Now + TimeValue("0:00:02"))

Set button = form(0).onsubmit

form(0).submit

'wait for page to load

While ie.Busy

DoEvents

Wend

Application.Wait (Now + TimeValue("0:00:02"))

Set var1 = ie.document.getElementById("resultStats")

Cells(x, 2).Value = var1.innerText

ie.Quit

Set ie = Nothing

Next x

 

End Sub

Your code will look like below:

Picture-6 Coding

(Picture 6) google search using Excel VBA

You will notice that the code will open your IE browser in a new window and then search all the keywords one by one and fetch the result and paste the result in excel sheet and when it is done it will close the browser.

Finally when it is done then go back to excel sheet and it will find that it has copied the search result and paste in desired cells.

Picture-7 Answers in Excel Cells

(Picture 7) SEARCH RESULTS USING EXCEL VBA

Please download the file from here and go through the code. What I have done is I have created an object called Internet Explorer and then navigating the page to Google. I waited for some time there until and unless this page comes to a ready state and then finally I used the ids to fetch the records.

Hope you have learned something new on my blog. Please go through the code and try to understand the logic. Also please let us know your feedback. Your comment will help to write some interesting blogs like this. You can learn more about our Advanced Excel VBA tutorials and Free Excel VBA tutorials