Skip to main content

Excel Macro: Search on Google

If you want to use VBA to open Google Chrome and search keywords on Google, the following Macro do this.

Search on Google

Sub SearchOnGoogle()
'Step 1: Declare your Variables
    Dim objShell As Object
    Dim SearchString As String
    
'Step 2: Define the objShell
    Set objShell = CreateObject("Shell.Application")
    
'Step 3: Input search keywords
    SearchString = InputBox("Enter your search keywords here", "Google Search")
    SearchString = Replace(SearchString, " ", "+")
    
'Step 4: Search keywords on Google
    Call objShell.ShellExecute("chrome.exe", "-url https://google.com/search?q=" & SearchString, "", "open", 1)
End Sub

How This Macro Works

1. Step 1: declares your Variables

2. Step 2: define the objShell

3. Step 3: input search keywords, and replace all space with +

4. Step 4: uses Shell.ShellExecute method to open Google Chrome to search the keywords. If you want to open FireFox to search on Google, please replace chrome.exe with firefox.exe:

Call objShell.ShellExecute("firefox.exe", "-url https://google.com/search?q=" & SearchString, "", "open", 1)

Alternative method

This method uses Shell function to open Google Chrome to search the keywords.

'------------------ Modules ------------------
Sub GoogleSearch()
'Step 1: Declare your Variables
    Dim ChromePath As String
    Dim SearchString As String

'Step 2: Location of Chrome.exe in your PC
    'Location of Win64 Chrome.exe in your PC
    ChromePath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
    If Dir(ChromePath, vbDirectory) = "" Then
        'Location of Win32 Chrome.exe in your PC
        ChromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
        If Dir(ChromePath, vbDirectory) = "" Then
            MsgBox "Chrome is not installed in the default directory."
            Exit Sub
        End If
    End If
'Step 3: Search keywords   
    SearchString = InputBox("Enter your search keywords here", "Google Search")
    SearchString = Replace(SearchString, " ", "+")

'Step 4: Search keywords on Google
    Shell (ChromePath & " -url https://google.com/search?q=" & SearchString)
End Sub

Most VBA code should be placed in Standard Modules unless specified.

If you see a comment '------------------ Modules------------------ in the code header that means put the code in a Standard Module. For more information, learn this course: Where should I put the Excel VBA code?

The following steps teach you how to put VBA code into a Standard Module:

  1. Activate the Visual Basic Editor by pressing ALT + F11.
  2. Right-click the project/workbook name in the Project Window.
  3. Choose Insert -> Module.
  4. Type or paste the code in the newly created module. You will probably need to change the sheet name, the range address, and the save location.
  5. Click Run button on the Visual Basic Editor toolbar.
  6. For more information, learn this course: Programming with Excel VBA

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>