0

I am trying to create VBA code to complete an automated Google batch search of multiple names, to include the following :

  1. First name (A1 cell), plus Surname (B1 cell) [next name in A2 and B2 cells, and so on and so forth]
  2. and "corruption" or "fraud" or "money laundering" (C1 cell)

Final desired Google search : "John Doe" AND "fraud" OR "corruption" OR "money laundering"

  1. 3 second delay between each Google search
  2. Command search button
  3. I would like to input the first and last name in the Spreadsheet, not the Macro code

This is the current code I have created using online tutorials :

Sub Google_search_using_google_chrome()

    Dim chromePath As String
    Dim searchTerm As String
        
    chromePath = """C:\Users\ianhe\AppData\Local\Google\Chrome\Application\chrome.exe"""
    
    searchTerm = Sheet1.Range("A1").Value & Sheet1.Range("B1").Value & Sheet1.Range("C1").Value
    
    searchkeyword = Replace(searchkeyword, " ", "+")
    
    Call Shell(chromePath & " -url https://www.google.com/search?q=" & searchTerm, vbNormalFocus)
End Sub

The current result I get is the following : JohnDoeandcorruption or "fraud i.e. there is not space between words, and only 1 quotation mark. When I include multiple words in the same cell, only the first code is taken into account.

1
  • 4
    You populate searchTerm but after that you switch to searchkeyword to replace spaces, then you finally use searchTerm ? Also what spaces are you replacing? Are there spaces in A1:C1? Commented Apr 18 at 15:55

1 Answer 1

2

More like this maybe:

Sub Google_search_using_google_chrome()

    Dim chromePath As String
    Dim searchTerm As String, fn As String, ln As String, rest As String
        
    chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""
    
    fn = Sheet1.Range("A1").Value
    ln = Sheet1.Range("B1").Value
    rest = Sheet1.Range("C1").Value
    
    searchTerm = """" & fn & " " & ln & """ " & rest
    
    searchTerm = Replace(searchTerm, " ", "+")     'spaces
    searchTerm = Replace(searchTerm, """", "%22")  'quotes

    'or use the built-in `EncodeURL` function in place of `Replace`
    'searchTerm = Application.EncodeURL(searchTerm)

    Debug.Print searchTerm
    
    Call Shell(chromePath & " -url https://www.google.com/search?q=" & searchTerm, vbNormalFocus)
End Sub

Input range:

inputs

Result:

result

Sign up to request clarification or add additional context in comments.

2 Comments

Amazing - it worked. Thanks, Tim. That said, I tried replicating the part of the code for a second name (Jane Doe), but it ignores the first search (John Doe) and only completes the second : fn = Sheet1.Range("A1").Value ln = Sheet1.Range("B1").Value rest = Sheet1.Range("C1").Value fn = Sheet1.Range("A2").Value ln = Sheet1.Range("B2").Value rest = Sheet1.Range("C1").Value
If you want to do multiple searches then you need to use some kind of loop... If you have follow-on questions involving code then it's best to edit your post and add the content there. Code in comments is difficult to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.