• Home  / 
  • VBA
  •  /  Perform Google Search on Internet Explorer using Excel VBA | Excel VBA

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

The blog relates with how to search data in Google using Excel VBA and take how many search result it is fetching.

For example, for you write “Apple” in Google search box and press enter and then it will show you the statistics that how many records it has populate and also how much time it has taken to fetch the records.

Picture 1

(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

(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)

(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)

(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)

(Picture 5) google search using excel vba

Here is the code 

Your code will look like below:

(Picture 6)

(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)

(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

Comment below regarding Userform

About the author

Niladri Sekhar Biswas

Hi, I have 9 years of experience working in Microsoft Excel. I am also an Excel and VBA corporate trainer. Writing about Excel and sharing my knowledge is my passion. I hope you learn more about Excel from my articles.

  • Martijn Rikkert

    Is there also an option to save every opened page as an pdf?

  • Keldon Moodley

    Hi!

    I keep getting an “Object Required” warning at this line: “ie.Document.getElementById(“lst-ib”).Value = var”

    Please assist and thank you for your time

  • Peter

    Hi!

    First of all great macro!!! I have a question regarding your VBA script, when I am running the VBA macro I get a bug if there is no result on a given search. Is it possible to make the macro continue regardless of there is a result or not?

    /Peter

    • Niladri Biswas

      Add if condition after Set var1 = ie.document.getElementById(“resultStats”)

      if var1.innertext “” then
      Cells(x, 2).Value = var1.innerText
      end if

      • Peter

        Thanks for the fast reply!
        I have tried to run the script with the extra code, however, it seems like it makes excel stop running. I am looking forward to hear if you can make it work.

        • Niladri Biswas

          Thanks for your reply!

          Check whether the id exists in the page or not. if not exists then pass the next one. Here is the code

          If IsObject(ie.document.getElementById(“resultStats”)) Then
          Set var1 = ie.document.getElementById(“resultStats”)
          Cells(x, 2).Value = var1.innerText

          Else
          Cells(x, 2).Value = “No Result Found”

          End If

          Please revert for any clarification. I have tested the code and it is running smoothly.

          • Peter

            It works perfectly, thanks!