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 going to 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 Google_Search_32()
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
Window 64 Version
Sub Google_Search_64()
Dim chromePath As String
Dim search_string As String
Dim query As String
query = InputBox("Enter here your search here", "Google Search")
search_string = Replace(query, " ", "+")
chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""
Shell (chromePath & " -url http://google.com/search?q=" & search_string)
End Sub
How to use this VBA Code to Search on Google
Here is the simple step you need to follow to use this code.
Open VB editor using the shortcut key Alt + F11 or go to the developer tab.
- Insert a new module in and paste above code into it.
Now, close VB editor and run this macro from the macros option in the 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 results for the query you have entered.
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!
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
IT GIVES RUN TIME ERROR 5
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
im getting error on the last line
When I run this, no matter what I type into the imputbox, I do not get any chrome connection.
Laurence
Many thanks.
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 ?
Nice work – Thanks
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”
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.
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
last line is given error kindly clear us.
Shell (chromePath & ” -url http://google.com/#q=” & search_string)
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
Thanks for sharing, Aaron. 🙂
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.
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.
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”
this code is having error in last line Shell (chromePath & ” -url http://google.com/#q=” & search_string)
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