How to Search on Google using a VBA Code


There are a lot of important things which we all do other than using Excel.

Am I right?

One of those things is using Google to search for something.

  • Insert a new module in and paste above code into it.  

Now, close VB editor and run this macro from macros option in developer tab.

How it works

When you run this code it shows you an input box to enter your query. And you need to enter your query in that input box and click OK.

Once you click OK, this code creates a search URL syntax using the text you have entered.

And, in the end redirects Chrome to that URL to give you result for query you have entered.

About the Author

Puneet Gogia

Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can find him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.

13 thoughts

Leave a Comment

Your email address will not be published.

  1. When I run this, no matter what I type into the imputbox, I do not get any chrome connection.
    Laurence

    Reply
  2. Hi, I get the imputbox and enter “Miles Davis”but the macro halts on this line
    Shell (chromePath & ” -url http://google.com/#q=” & search_string)
    If I get it to run with your advice could I eliminate the Imputbox and have the search be the current cell contents ?

    Reply
  3. I have a string like this below with multiple keywords that i need to search using different names in google and copy the address bar link after search in excel cell . is there a way to automate this and do that with a help of macro??

    “NAME” AND “fun” OR “movie” OR “entertainment” OR “dace” OR “movie”

    Reply
  4. I try the code and it runs well.
    Thank you very much.

    Now I wonder how do I “click” the first result via VBA.
    It seems need a code something like “getElementBy….().click”,
    but I don’t know how to implement it.

    Thank you once again.

    Reply
  5. Update the last part ”chromePath = “C:Program Files (x86)GoogleChromeApplicationchrome.exe” to as follows

    chromePath = “C:\Program Files (x86)\Google\Chrome\Application\chrome.exe”

    Dont forget to put the “\” as above.

    It works like wonder

    here is the corrected code that works for me for 64bith Windows
    ——–
    Sub Search()
    Dim chromePath As String
    Dim search_string As String
    Dim query As String
    query = InputBox(“Enter here your search here”, “Google Search”)
    search_string = query
    search_string = Replace(search_string, ” “, “+”)
    chromePath = “C:\Program Files (x86)\Google\Chrome\Application\chrome.exe”
    Shell (chromePath & ” -url http://google.com/#q=” & search_string)
    End Sub

    Reply
  6. Found another route based on your code sir. Which by the way is excellent. My code takes the shell command out, and will open your default browser with the value you entered in your search string.

    Sub GoogleSearch()

    Dim MyURL As String
    Dim search_string As String
    Dim query As String

    query = InputBox(“Enter here your search here”, “Google Search”)
    search_string = query
    search_string = Replace(search_string, ” “, “+”)
    MyURL = “https:/google.com/#q=”

    ActiveWorkbook.FollowHyperlink Address:=MyURL & search_string
    End Sub

    Reply
  7. This is nice thing to work, can we have some modifications to pick the keyword from a range and save the link in new column after searching.

    Reply
  8. The code needs a little tweaking, but I found it also works for FireFox, therefore probably any browser or application.

    Your paths in the example code for copying are missing some slashes, , but they are present in the screen capture.

    In the “32 Bit” windows version there is no need for the …/program files (86)/… code . That is (part of) the path for 32 bit applications installed on 64 bit Windows.

    In the 64 bit version, I tweaked the comment a little to make it clearer (to me)

    Like your screen capture version, I prefer to use blank lines to space out blocks of code.

    ‘ Uncomment the following line for Windows 64 and FF 32, comment out FF 64 version (above)

    Sub Google_Search_FF()

    Dim FFPath As String
    Dim search_string As String
    Dim query As String

    query = InputBox(“Enter here your search here”, “Google Search”)
    search_string = query
    search_string = Replace(search_string, ” “, “+”)

    ‘ Uncomment the following line for Windows 64 and FF 64, comment out FF 32 version (below)
    ‘ FFPath = “C:Program FilesMozilla Firefoxfirefox.exe”

    ‘ Uncomment the following line for Windows 64 and FF 32, comment out FF 64 version (above)
    FFPath = “C:Program Files (x86)Mozilla Firefoxfirefox.exe”

    ‘ Run the program, passing the search code
    Shell (FFPath & ” -url http://google.com/#q=” & search_string)

    End Sub

    Keep up the good work.

    Reply