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.

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

2 Responses

Related Tutorials

Delete Duplicate in Excel or Remove Duplicate in Excel
November 9, 2018
Excel Formulas PDF
September 6, 2018
How To Lock Cells in Excel | Unprotect Excel
August 13, 2018
4x Faster at Excel
August 6, 2018
Separate Content of One Excel Cells into Separate Columns
August 3, 2018
How to Transpose Excel Columns to Rows | Paste Special Method
July 26, 2018
How to create sparklines in Excel
July 19, 2018
AutoSum in Excel with Shortcut
July 17, 2018
OFFSET Function in Excel
July 6, 2018
Strikethrough Shortcut in Excel & Word
July 4, 2018
INDIRECT Function with SUM, MAX, MIN & Independent Cell Value
June 29, 2018
Pivot Table Slicers In Excel
June 12, 2018
How to Split Cells in Excel using Text to Column
June 7, 2018
How to Wrap Text in Excel Automatically and Manually
June 6, 2018
How to Hide/Unhide Column in Excel
June 5, 2018
Highlight row based on cell value
June 4, 2018
Learn how to remove blank cells in Excel
June 3, 2018
How to Group Numbers, Dates & Text in Pivot table in Excel
June 1, 2018
5 Powerful Tricks to Format cells in Excel
May 31, 2018
Insert a Picture into a Cell in Excel
May 25, 2018
What is ISFORMULA Function and FORMULATEXT Function
May 21, 2018
How to Use SUBSTITUTE Function
May 21, 2018
Excel Quartile Function in Excel
May 8, 2018
How to use the Excel PERCENTILE function
May 7, 2018
Insert or Type degree symbol in Excel with Autocorrect Feature
May 7, 2018