【问题标题】:Excel VBA address lookupExcel VBA 地址查找
【发布时间】: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


【解决方案1】:

尝试将“.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 来获取它的价值。

【讨论】:

  • "Value" 是 Range 的默认属性:在大多数情况下,您可以不使用它而不会造成任何问题(但最好包含它)
  • 我仍然得到:运行时错误 '438' 对象不支持此属性或方法
  • 您的第一个工作表可以是图表而不是工作表吗?尝试使用 Worksheets 集合而不是 Sheets 集合来避免图表。
  • 真的……没想到。关于 DHuber 的代码为什么“出错”,我能想到的唯一其他原因是没有活动的工作簿,因此 Sheets(1) 不是有效对象。
  • 哇,我确实有一个图表,这导致了错误!完成后,我收到一条消息,上面写着“已完成拉链”,但我没有看到它们被记录在任何地方。我错过了什么吗!
猜你喜欢
  • 2020-11-03
  • 1970-01-01
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多