By: Lutfor Rahman Shimanto

In reply to Olaide.

Hello Olaide

Thanks for thanks! You are very welcome. Your appreciation means a lot to us.

You are right! The user-defined function works perfectly fine for a few sets of coordinates. I have reviewed the code and estimated the time required to perform it on a pair of coordinates; it takes 5.7 seconds on my machine (the time can vary from device). It will take almost 8 hours to perform 5000 sets of coordinates with a spontaneous internet connection.

The user-defined function mentioned in this article uses the OpenStreetMap Nominatim API to perform reverse geocoding. It retrieves location information based on the lat and long values given to it. It must take the required time to perform perfectly.

So, it is impossible to reverse geocode 5000 sets of coordinates within minutes. If you ever feel like getting location information without applying the user-defined function like the regular Excel function, you can use the following sub-procedure. You need to run the code, and it will consider the lat and long values as columns B and C starting from row 3 and display the result in column D.

Sub PopulateReverseGeocoding()

Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long

Set ws = ThisWorkbook.Sheets("Sheet1")

lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row

For i = 3 To lastRow

Dim lat As Double
Dim lon As Double
lat = ws.Cells(i, "B").Value
lon = ws.Cells(i, "C").Value

ws.Cells(i, "D").Value = ReverseGeocoder(lat, lon)

Next i

End Sub

Function ReverseGeocoder(lati As Double, longi As Double) As String
On Error GoTo ErrorHandler

Dim xD As New MSXML2.DOMDocument
Dim URL As String

xD.async = False
URL = "https://nominatim.openstreetmap.org/reverse?lat=" + CStr(lati) + _
"&lon=" + CStr(longi)

xD.Load (URL)

If xD.parseError.ErrorCode <> 0 Then
ReverseGeocoder = xD.parseError.reason
Else
xD.SetProperty "SelectionLanguage", "XPath"
Dim loca As MSXML2.IXMLDOMElement
Set loca = xD.SelectSingleNode("/reversegeocode/result")

If loca Is Nothing Then
ReverseGeocoder = "No location found."
Else
ReverseGeocoder = loca.Text
End If
End If

Exit Function

ErrorHandler:
ReverseGeocoder = "Error occurred: " & Err.Description

End Function

I hope you understand the situation. Stay blessed.

Regards
Lutfor Rahman Shimanto
Excel & VBA Developer
ExcelDemy