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
- Write “Apple” in the 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.
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 ⇓
Now Press on the arrow (Red circle in Picture 2). And you are forced to go back to the Google page. Then place your on the Google search box and then click on the box. As soon as you click on the box again you will be on the HTML page and this time, you will get everything off that box like name, title, id, etc.
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 the excel file is like the below picture
Now we will take them 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:
- Open the VBA page by pressing ALT + F11
- Go to Insert and then Module
- Copy the below code and paste it into the Module
- Go to Tools and then Reference and select all the references as shown in the Picture
- Run the code by pressing F5 or from the Run button
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 End Sub
Your code will look like below:
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 into an excel sheet when it is done it will close the browser.
Finally when it is done then go back to the excel sheet and it will find that it has copied the search result and pasted in desired cells.
What I have done is I have created an object called Internet Explorer and then navigated 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 from 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.