繁体   English   中英

Excel VBA地址查找

[英]Excel VBA address lookup

我正在尝试验证数千个地址的列表。 我发现此代码应该将我的电子表格数据与Google Maps 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

尝试将“ .Value”添加到“ Do”行及其后面的行:

Sub BatchGeocode()
With Sheets(1)
    r = 2
    Do While .Cells(r, 1).Value <> ""
        .Cells(r, 2).Value = getzip(.Cells(r, 1).Value)

Cells(r,1)是一个对象,而不是一个值。 使用.Value来获取它的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM