How to Search on Google using a VBA Code

[wp_ad_camp_2]

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.

In my list of useful macro codes, I have a code which you can use to perform a search query on Google using Chrome.

And today, I’m gonna share that code with you.

You can add this code to your personal macro workbook and use it anytime when you need to search for something on Google.

[wp_ad_camp_3]

VBA Code to Open Google Chrome for Search

Here is the code below which you can use to search on Google using Chrome.

Window 32 Version

Sub SearchWindow32()Dim chromePath As StringDim search_string As StringDim query As Stringquery = InputBox(“Enter here your search here”, “Google Search”)search_string = querysearch_string = Replace(search_string, ” “, “+”)’Uncomment the following line for Windows 64 versions and comment out Windows 32 versions’chromePath = “C:Program FilesGoogleChromeApplicationchrome.exe”‘Uncomment the following line for Windows 32 versions and comment out Windows 64 versionschromePath = “C:Program Files (x86)GoogleChromeApplicationchrome.exe”Shell (chromePath & ” -url http://google.com/#q=” & search_string)End Sub

Window 64 Version

Sub SearchWindow64()Dim chromePath As StringDim search_string As StringDim query As Stringquery = InputBox(“Enter here your search here”, “Google Search”)search_string = querysearch_string = Replace(search_string, ” “, “+”)’Uncomment the following line for Windows 64 versions and comment out Windows 32 versionschromePath = “C:Program FilesGoogleChromeApplicationchrome.exe”‘Uncomment the following line for Windows 32 versions and comment out Windows 64 versions’chromePath = “C:Program Files (x86)GoogleChromeApplicationchrome.exe”Shell (chromePath & ” -url http://google.com/#q=” & search_string)End Sub

How to use this VBA Code to Search on Google

Here are the simple step you need to follow to use this code.

  • Open VB editor using shortcut key Alt + F11 or go to developer tab.

  • 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.

More VBA Codes to Try

About the Author

puneet one point one

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.

20 thoughts on “How to Search on Google using a VBA Code”

  1. cool ! James you’ve got this !
    Why in the last time it worked, maybe google search engine did something changes that turn out the old code does not work with that search anymore.
    Using yours method would resolve the issue.
    Good work!

    Reply
  2. Hi Puneet
    Can you please help me with the code for reading Edge Browser URL without Selenium through VBA.
    Requirement is to read all the tabs open in the Edge Brower through VBA.
    Kindly Help

    Reply
    • Try this.
      chrome.exe paths needed help
      and
      ” -url http://google.com/#q=” should be ” -url http://google.com/search?q=

      Sub SearchWindow()
      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/search?q=” & search_string)

      End Sub

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

    Reply
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
    • I found the same to be true for Chrome, i.e., 32-bit Chrome in 64-bit Windows requires path with “Program Files (x86)” and 64-bit Chrome in 64-bit Windows requires “Program Files”

      Reply
    • Here’s the correct code for Windows 64, Chrome

      Sub SearchWindow64()
      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, ” “, “+”)
      MyURL = “https://www.google.com/search?q=”
      ‘chromePath = “C:Program Files (x86)GoogleChromeApplicationchrome.exe”
      ActiveWorkbook.FollowHyperlink Address:=MyURL & search_string
      End Sub

      Reply

Leave a Comment