I am attempting to validate a list of several thousand addresses. I found this code which should compare my spreadsheet data into a google maps api, but I am erroring out on line 'Do While .Cells(r, 1) <> "". Can anyone help me out? I did enter a correct API code "AIzaSyBXhfKRfc2BgWeF5snXswLsvvdUYprhc2k" in that first line.
Const MYKEY = "your_key_goes_here"
Sub BatchGeocode()
With Sheets(1)
r = 2
Do While .Cells(r, 1) <> ""
.Cells(r, 2) = getzip(.Cells(r, 1).Value)
'the next 4 lines ensure that we don't abuse Google by querying them too fast
t = Timer
Do While Timer < t + 0.3
DoEvents
Loop
r = r + 1
Loop
End With
MsgBox "Done getting zips"
End Sub
Function getzip(myAddress As String) As String
myAddress = Replace(myAddress, " ", "+")
myURL = "http://maps.google.com/maps/geo?q=" & myAddress & "&output=xml&oe=utf8&sensor=false&key=" & MYKEY
Dim objHttp As Object
Set objHttp = CreateObject("MSXML2.ServerXMLHTTP")
Call objHttp.Open("GET", myURL, False)
Call objHttp.Send("")
Results = objHttp.ResponseText
sloc = InStr(Results, "<PostalCodeNumber>") + Len("<PostalCodeNumber>")
eloc = InStr(sloc, Results, "</PostalCodeNumber>")
If eloc > sloc Then getzip = Mid(Results, sloc, eloc - sloc) Else getzip = ""
End Function