Excel VBA 地址查询

Excel VBA address lookup

我正在尝试验证包含数千个地址的列表。我发现这段代码应该将我的电子表格数据与 google 映射 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 获取它的值。