【发布时间】:2017-05-25 06:25:06
【问题描述】:
我正在尝试验证包含数千个地址的列表。我发现这段代码应该将我的电子表格数据与谷歌地图 api 进行比较,但我在“Do While .Cells(r, 1) "" 线上出错了。谁能帮我吗?我确实在第一行输入了正确的 API 代码“AIzaSyBXhfKRfc2BgWeF5snXswLsvvdUYprhc2k”。
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
【问题讨论】:
-
"erroring out" 到底是什么样的?如果单元格包含错误,您将得到“类型不匹配”
-
我怀疑当 you exceed the usage limit 用于 maps API 时会发生“错误输出”。
标签: excel vba street-address