VBA - Google search string in default browser

spidaman

Board Regular
Joined
Jul 26, 2015
Messages
70
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
 

BiocideJ

Well-known Member
Joined
Jan 23, 2012
Messages
1,733
I believe the corrected line of code is this:

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

spidaman

Board Regular
Joined
Jul 26, 2015
Messages
70
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 "......"
 

BiocideJ

Well-known Member
Joined
Jan 23, 2012
Messages
1,733
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:

spidaman

Board Regular
Joined
Jul 26, 2015
Messages
70
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.
 

Forum statistics

Threads
1,078,544
Messages
5,341,086
Members
399,417
Latest member
Andrea Elyas

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top