Chihiro
Excel Ninja
Since Google API has query limit. I typically pilot IE and use following.
Assuming you have table1 like below.
Since this process is prone to crashing after running it for a while. I tend to limit it to 5000 or less per run.
Result:
Assuming you have table1 like below.
Code:
Function GetCurrentURL()
Dim vUrl As String
Set SWs = New SHDocVw.ShellWindows
For Each vIE In SWs
If TypeName(vIE.document) = "HTMLDocument" Then
vUrl = vIE.document.Url
Exit For
End If
Next
GetCurrentURL = vUrl
End Function
Sub LatLong()
Dim cel As Range
Dim sStr As String, ltlng As String
Dim iMax As Long, i As Long: i = 0
Dim IE As InternetExplorer
iMax = 5000
For Each cel In Sheet1.Range("Table1[Street]").SpecialCells(xlCellTypeVisible).Cells
If Len(cel.Offset(, 2).Value) = 0 Then
sStr = cel.Value & ", " & cel.Offset(, -1).Value
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate "www.google.ca/maps?q=" & sStr
While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend
Application.Wait (Now + TimeValue("0:00:05"))
ltlng = Split(Split(GetCurrentURL(), "@")(1), ",17z")(0)
cel.Offset(, 2) = CDbl(Split(ltlng, ",")(0))
cel.Offset(, 3) = CDbl(Split(ltlng, ",")(1))
IE.Quit
Set IE = Nothing
i = i + 1
If i = iMax Then Exit For
End If
Application.StatusBar = "Progress: " & i & " of " & iMax & " " & Format(i / iMax, "Percent")
Next
Application.StatusBar = False
End Sub
Since this process is prone to crashing after running it for a while. I tend to limit it to 5000 or less per run.
Result:
Last edited: