VBA Code to Search on Google

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.

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 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 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 versions
chromePath = "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 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 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 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.

Sample File

Download this sample file from here to try it your self.

More VBA Codes to Try

  1. ON-OFF Button in Excel
  2. Internet Radio in Excel
  3. A List of 100 Macro Codes
  4. Create Table of Content
  5. Macro Code To Create a Pivot Table 

  • Pankaj Bahuguna

    this code is having error in last line Shell (chromePath & ” -url http://google.com/#q=” & search_string)

  • Ron007

    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.

  • Ankit Gupta

    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.