How to populate google search results to worksheet in Excel?
In some cases, you may need to do some important keyword search in Google and keep the top searching result record in a worksheet which includes the title and hyperlink of the article. This article provides a VBA method to help populate google search results to a worksheet based on given keywords in cells.
Populate google search results to worksheet with VBA code
Populate google search results to worksheet with VBA code
Supposing the keywords you need to search listing in column A as below screenshot shown, please do as follows to populate the google search results of these keywords to corresponding columns with VBA code.
1. Press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.
2. In the Microsoft Visual Basic for Applications window, please click Insert > Module. Then copy and paste VBA code into the code window.
VBA code: populate google search results to worksheet
Sub xmlHttp()
'Updated by Extendoffice 2018/1/30
Dim xRg As Range
Dim url As String
Dim xRtnStr As String
Dim I As Long, xLastRow As Long
Dim xmlHttp As Object, xHtml As Object, xHtmlLink As Object
On Error Resume Next
Set xRg = Application.InputBox("Please select the keywords you will search in Google:", "KuTools for Excel", Selection.Address, , , , , 8)
If xRg Is Nothing Then Exit Sub
Application.ScreenUpdating = False
xLastRow = xRg.Rows.Count
Set xRg = xRg(1)
For I = 0 To xLastRow - 1
url = "https://www.google.co.in/search?q=" & xRg.Offset(I) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)
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 xHtml = CreateObject("htmlfile")
xHtml.body.innerHTML = xmlHttp.ResponseText
Set xHtmlLink = xHtml.getelementbyid("rso").getelementsbytagname("H3")(0).getelementsbytagname("a")(0)
xRtnStr = Replace(xHtmlLink.innerHTML, "<EM>", "")
xRtnStr = Replace(xRtnStr, "</EM>", "")
xRg.Offset(I, 1).Value = xRtnStr
xRg.Offset(I, 2).Value = xHtmlLink.href
Next
Application.ScreenUpdating = True
End Sub
3. Press the F5 key to run the code. In the popping up Kutools for Excel dialog box, please select the cells containing keywords you will search, and then click the OK button. See screenshot:
Then all search results including titles and links are populated into corresponding column cells based on keywords. See screenshot:
Related articles:
- How to populate a combo box with specified data on Workbook open?
- How to auto populate other cells when selecting values in Excel drop down list?
- How to auto populate other cells when selecting values in Excel drop down list?
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!






