1

I'm trying to implement a Google Search method using VBA in Excel.

I would like to type a certain word/words in the pop-up window, whereupon it Google searches the word/words + a predetermined phrase.

For example, if I type "John Doe", I would like the Google search to be "John Doe License and Registration", without having to type "License and Registration" every time I type a name.

How do I add the "License and Registration" in my code?

Private Sub CommandButtonSearch_Click()
    Dim query As String
    Dim search_string As String
    Dim googleChromePath As String
    
    query = InputBox("Enter your keyword", "Google Search")
    search_string = Replace(query, " ", "+")
    
    googleChromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
    
    Shell (googleChromePath & " -url http://google.com/search?q=" & search_string)

End Sub

How it looks:
enter image description here

How I would like it to look:
enter image description here

2 Answers 2

1
Private Const LicenseRegistration As String = "+License+and+Registration"
Private Sub CommandButtonSearch_Click()
    Dim query As String
    Dim search_string As String
    Dim googleChromePath As String
    
    query = InputBox("Enter your keyword", "Google Search")
    search_string = Replace(query, " ", "+") & LicenseRegistration
    
    googleChromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
    
    Shell (googleChromePath & " -url http://google.com/search?q=" & search_string)

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

2 Comments

thank you very much, that's exactly what I was looking for. Sorry to bother you with a second question, but do you happen to know a way to store all Google searches from Excel in either the same Excel file but in another sheet, or a completely different file? Thanks again!
sorry to burden you again, but did you get a chance to look at the second question I had a problem with?
0

Something like this, perhaps.

Sub Gethits()
    Dim url As String, lastRow As Long
    Dim XMLHTTP As Object, html As Object, objResultDiv As Object, objH3 As Object, link As Object
    Dim start_time As Date
    Dim end_time As Date
    Dim var As String
    Dim var1 As Object

    lastRow = Range("A" & Rows.Count).End(xlUp).row

    Dim cookie As String
    Dim result_cookie As String

    start_time = Time
    Debug.Print "start_time:" & start_time

    For i = 2 To lastRow

        url = "https://www.google.com/search?q=" & Cells(i, 1)
        Set XMLHTTP = CreateObject("MSXML2.serverXMLHTTP")
        XMLHTTP.Open "GET", url, False
        XMLHTTP.setRequestHeader "Content-Type", "text/xml"
        XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
        XMLHTTP.send

        Set html = CreateObject("htmlfile")
        html.body.innerHTML = XMLHTTP.ResponseText
        Set objResultDiv = html.getElementById("rso")
        Set var1 = html.getElementById("result-stats")
        Cells(i, 2).Value = var1.innerText

        DoEvents
    Next

    end_time = Time
    Debug.Print "end_time:" & end_time

    Debug.Print "done" & "Time taken : " & DateDiff("n", start_time, end_time)
    MsgBox "done" & "Time taken : " & DateDiff("n", start_time, end_time)
End Sub

Result:

enter image description here

Comments

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.