VBA - Google search string in default browser

spidaman

Board Regular
Joined
Jul 26, 2015
Messages
116
Office Version
  1. 365
Platform
  1. Windows
Can anyone help with this please?

This VBA project is meant to create a Google search string which opens directly in Google search engine in the users default browser. At the moment the search string is not producing the double speech marks around EACH value (which i want to keep). So if the value entered in the input box is 123456 the search string is: "123456 | 12". I am looking for this: "123456" | "12"

Code:
Sub Google_Search_v1()


Dim Target As Variant
Dim LValue1 As String
Dim MyURL As String


MyURL = "https:/google.com/#q="


    Target = Application.InputBox("Enter Number: ")
    
    If Target = 0 Then Exit Sub
    
    LValue1 = """""" & Target & """" & " | " & """" & Left(Target, 2) & """"""
        
    ActiveWorkbook.FollowHyperlink Address:=MyURL & LValue1


End Sub

Any help is much appreciated.
Thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I believe the corrected line of code is this:

Code:
LValue1 = """" & Target & """ | """ & Left(Target, 2) & """"
 
Upvote 0
I believe the corrected line of code is this:

Code:
LValue1 = """" & Target & """ | """ & Left(Target, 2) & """"

Thanks for your help, but I'm afraid that hasn't produced the desired result. The string produced in Google search engine was: 123456 | 12

So missing the "......"
 
Upvote 0
Thanks for your help, but I'm afraid that hasn't produced the desired result. The string produced in Google search engine was: 123456 | 12

So missing the "......"

So it does. I didn't take into account that the web address has an additional level of text parsing so you basically need to double up all of your quotes.
Also, I noticed that if your Target value has a space that it causes this to also not work. This is the reason I added the Replace(...," ", %20). You may need to add additional replaces if other text characters are truncating the search text.

Here is the TESTED correct line of code:
Code:
LValue1 = Replace("""""" & Target & """"""" | """"""" & Left(Target, 2) & """""", " ", "%20")
 
Last edited:
Upvote 0
So it does. I didn't take into account that the web address has an additional level of text parsing so you basically need to double up all of your quotes.
Also, I noticed that if your Target value has a space that it causes this to also not work. This is the reason I added the Replace(...," ", %20). You may need to add additional replaces if other text characters are truncating the search text.

Here is the TESTED correct line of code:
Code:
LValue1 = Replace("""""" & Target & """"""" | """"""" & Left(Target, 2) & """""", " ", "%20")

BiocideJ thanks very much for posting this solution; it now works perfectly. Your help is much appreciated.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top