I have a list of over 50,000 places in Excel (eg. Arches National Park, Cornell Museum of Glass). When I type “Cornell Museum of Glass” into Google, it returns an address of “1 Museum Way, Corning, NY 14830”. I need the full street address Street #, Street name, and postal code. I do have the state NY though(in Excel). Does anyone know how to search the address on Google(typing Cornell Museum of Glass), then return the street address back onto Excel(1 Museum Way, Corning, NY 14830)? I know VBA is capable of doing this, and I don’t want to manually do it 50,000 times, plus I’m a total newbie at VBA. If anyone knows how I would greatly appreciate it! Thank you in advance.
The following should be the information needed to solve the problem
Here is a post that shows how to use VBA and XML to interact with Google Maps (via the google maps api).
And Here is google’s Place API, which is what you’re looking for (requesting/receiving information from google).
I have created a working test. You have to create a google API key, you can go here to do that. Once you have the API key, put it into the code and run it.
Note that Google does restrict how often an API key can be used each day. A free user seems to get 1000 “requests” to the “place” API each day. Annoyingly, some calls seem to be worth more “requests” than others. My code makes 2 calls to google’s “place” API, but google counts them as 11 requests. This is because a call to “place/textsearch” is worth 10 “requests”. My other call was to places/details is worth 1. All this means is that you’re limited to 90 entries each day/free api key with this method.
Finally, this was a terrific post for this topic, I based my code off it.
Sub myTest() Dim xhrRequest As XMLHTTP60 Dim domDoc As DOMDocument60 Dim domDoc2 As DOMDocument60 Dim placeID As String Dim query As String Dim nodes As IXMLDOMNodeList Dim node As IXMLDOMNode 'you have to replace spaces with + query = "Cornell+Museum+of+Glass" 'You must acquire a google api key and enter it here Dim googleKey As String googleKey = "imaginary api key" 'your api key here 'Send a "GET" request for place/textsearch Set xhrRequest = New XMLHTTP60 xhrRequest.Open "GET", "https://maps.googleapis.com/maps/api/place/textsearch/xml?" & _ "query=Cornell+Museum+of+Glass&key=" & googleKey, False xhrRequest.send 'Save the response into a document Set domDoc = New DOMDocument60 domDoc.LoadXML xhrRequest.responseText 'Find the first node that is called "place_id" and is the child of the "result" node placeID = domDoc.SelectSingleNode("//result/place_id").Text 'recycling objects (could just use new ones) Set domDoc = Nothing Set xhrRequest = Nothing 'Send a "GET" request for place/details Set xhrRequest = New XMLHTTP60 xhrRequest.Open "GET", "https://maps.googleapis.com/maps/api/place/details/xml?placeid=" & placeID & _ "&key=" & googleKey, False xhrRequest.send 'Save the response into a document Set domDoc = New DOMDocument60 domDoc.LoadXML xhrRequest.responseText Dim output As String Dim s As String 'hacky way to get postal code, you might want to rewrite this after learning more Set nodes = domDoc.SelectNodes("//result/address_component/type") For Each node In nodes s = node.Text If s = "postal_code" Then 'this is bad, you should search for "long_name", what i did here was assume that "long_name was the first child" output = vbNewLine & "Postal Code: " & node.ParentNode.FirstChild.Text End If Next node 'output MsgBox "Formatted Address: " & domDoc.SelectSingleNode("//result/formatted_address").Text & output End Sub
EDIT: Now using place API rather than Geocoding API
I had an error similar to a poster above that I fixed and wanted to post the fix somewhere as I am now using a modified version of this code which once it works I will also post. What was happening is I was passing names that google had no results for. If you may be in a similar boat the following line is a positive check for if you got something back: (the negitive is not = “ZERO_RESULTS”)
if domDoc.selectSingleNode("//status").Text = "OK"
You have to get an API key and then the code will work just fine. Go to the link below and click the button that reads ‘GET A KEY’.