π₯ Download the Templates
π’ Excel Geocoding Template (.xlsx)
β οΈ First Time?
This starter workbook works in any recent Excel. For the first time only, youβll import the VBA macro, then you can use it normally.
π
Download Excel Template (.xlsx)
(needs VBA macro)
β First-time setup (one-time)
- Open
Geocoding_Template.xlsx
and save it as .xlsm: File β Save As β Excel Macro-Enabled Workbook (*.xlsm). - Press ALT + F11 to open the VBA editor β Insert β Module.
- Paste the VBA from the βπ VBA Macro Codeβ section below into the new module.
- (Optional but recommended) Install the free VBA-JSON parser:
- Download from github.com/VBA-tools/VBA-JSON.
- In VBA editor: File β Import Fileβ¦, choose
JsonConverter.bas
. - In Tools β Referencesβ¦, tick βMicrosoft Scripting Runtimeβ if available.
- Back in Excel, enable macros if prompted: File β Options β Trust Center β Trust Center Settings β Macro Settings.
- Enter Latitude in column A and Longitude in column B.
- Run the macro: Developer β Macros β GetAddresses β Run.
- Keep at least 1 second between lookups (already handled in the sample macro) to respect Nominatim limits.
π VBA Macro Code (Reverse Geocoding)
Option Explicit
Sub GetAddresses()
Dim http As Object
Dim lat As Variant, lon As Variant, url As String, json As String
Dim i As Long, lastRow As Long
Application.ScreenUpdating = False
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow
lat = Cells(i, 1).Value
lon = Cells(i, 2).Value
If Len(lat) > 0 And Len(lon) > 0 Then
url = "https://nominatim.openstreetmap.org/reverse?format=json&lat=" & lat & "&lon=" & lon & "&zoom=18&addressdetails=1"
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", url, False
http.setRequestHeader "User-Agent", "ExcelGeocoder - basicexceltutorial.com"
http.Send
If http.Status = 200 Then
json = http.responseText
Cells(i, 3).Value = ExtractJsonValue(json, "display_name")
Else
Cells(i, 3).Value = "HTTP " & http.Status
End If
Application.Wait Now + TimeValue("0:00:01")
End If
Next i
Application.ScreenUpdating = True
End Sub
Function ExtractJsonValue(ByVal json As String, ByVal key As String) As String
Dim token As String, startPos As Long, valueStart As Long, valueEnd As Long, valText As String
token = """" & key & """:"""
startPos = InStr(1, json, token, vbTextCompare)
If startPos = 0 Then
ExtractJsonValue = "Not found"
Exit Function
End If
valueStart = startPos + Len(token)
valueEnd = InStr(valueStart, json, """")
Do While valueEnd > 0 And Mid$(json, valueEnd - 1, 1) = "\"
valueEnd = InStr(valueEnd + 1, json, """")
Loop
If valueEnd > valueStart Then
valText = Mid$(json, valueStart, valueEnd - valueStart)
valText = Replace(valText, "\/", "/")
valText = Replace(valText, "\\", "\")
ExtractJsonValue = valText
Else
ExtractJsonValue = "Not found"
End If
End Function
π Next time you use it
- Open your saved .xlsm workbook, enable macros, paste new coordinates, and run GetAddresses.
π‘ Google Sheets Template
Use this online template with Google Apps Script. No installation needed.
π Open Google Sheets Template β Copy of Geocoding (click to make a copy)
β First-time setup (one-time)
- Open the link and click Make a copy.
- Go to Extensions β Apps Script, confirm the
GetAddresses
function exists (paste the script below if needed), then click Run. - Complete the authorization prompts (Google will show the required scopes).
π Google Apps Script Code (Reverse Geocoding)
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Geocoding')
.addItem('Get Addresses', 'GetAddresses')
.addToUi();
}
function GetAddresses() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const startRow = 2;
const lastRow = sheet.getLastRow();
if (lastRow < startRow) return;
const data = sheet.getRange(startRow, 1, lastRow - startRow + 1, 2).getValues();
for (let i = 0; i < data.length; i++) {
const lat = data[i][0];
const lon = data[i][1];
if (lat !== '' && lon !== '') {
try {
const url = `https://nominatim.openstreetmap.org/reverse?format=json&lat=${encodeURIComponent(lat)}&lon=${encodeURIComponent(lon)}&zoom=18&addressdetails=1`;
const response = UrlFetchApp.fetch(url, {
headers: { 'User-Agent': 'GoogleSheetsGeocoder - basicexceltutorial.com' },
muteHttpExceptions: true
});
const result = JSON.parse(response.getContentText());
const display = (result && result.display_name) ? result.display_name : 'Address not found';
sheet.getRange(i + startRow, 3).setValue(display);
Utilities.sleep(1000);
} catch (e) {
sheet.getRange(i + startRow, 3).setValue('Error: ' + e.message);
}
}
}
}
βΆοΈ How to use
- Enter Latitude (col A) and Longitude (col B).
- Run
GetAddresses
from Extensions β Apps Script (or add a custom menu if you prefer). - Results will appear in column C. A 1-second delay is included to respect API limits.
π¦ Excel + Macro ZIP Package
This ZIP includes a macro-enabled workbook ready to go, plus the standalone VBA module.
π Download Excel Macro ZIP (macro pre-installed)
βΆοΈ How to use
- Extract the ZIP.
- Open the included .xlsm file, click Enable Content if prompted.
- Paste your coordinates (Lat in A, Lon in B) and run GetAddresses.
π§© Whatβs inside
- .xlsm workbook: macro already embedded (fastest option).
- .bas file: the macro module, in case you want to import it into another workbook (ALT+F11 β File β Import Fileβ¦).
βΉοΈ Notes, Limits & Troubleshooting
- API: Uses OpenStreetMapβs Nominatim (no API key). Respect 1 request/second; practical free usage is light/medium volume.
- Slow or βAddress not foundβ? Check your internet/firewall, confirm your coordinates are valid, and try again.
- Corporate environments: If web requests are blocked, ask IT to allow traffic to
nominatim.openstreetmap.org
.