Managing expansive address databases and filtering them by precise zip code radii in Excel is notoriously tedious and prone to manual error. While organizations often look to standard funding sources to acquire enterprise-grade GIS software, a native spreadsheet solution remains highly accessible. This mathematical approach grants users immediate, cost-free spatial filtering capabilities directly within their existing workflows.
A key stipulation, however, is that Excel requires pre-calculated latitude and longitude coordinates to compute great-circle distances accurately. Used successfully by regional delivery services to optimize local routing, this method bridges the gap between raw data and spatial insight. Below, we outline the exact formulas and setup steps to construct your radius filter.
In sales, marketing, and logistics, location intelligence is everything. Whether you are planning a localized direct mail campaign, assigning sales territories, or finding the closest distribution hubs to your customers, filtering an address database based on geographic proximity is a critical task. While dedicated Geographic Information System (GIS) software can do this, you do not need expensive tools to accomplish it. With a few creative formulas and a reference database, you can build a dynamic, radius-based zip code filter directly in Microsoft Excel.
This guide will walk you through setting up an interactive Excel model that calculates the distance between a target zip code and your entire address database, allowing you to instantly filter for records within a specified mile or kilometer radius.
Since Excel does not naturally understand geographic coordinates, we must provide it with two things: the latitude and longitude of our zip codes, and a mathematical formula to calculate the distance between those coordinates on a sphere.
To calculate the distance between two points on the Earth's surface, we use the Spherical Law of Cosines. While the Haversine formula is also popular, the Law of Cosines is slightly shorter and highly accurate for calculating terrestrial distances in standard spreadsheet applications. The mathematical formula is represented as:
d = acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1) ) * R
Where:
To implement this in Excel, you will need two tables within your workbook:
This is your primary list. It should contain columns like Customer Name, Address, City, State, and most importantly, the Zip Code. Let's assume this table is named tblCustomers.
You need a clean database containing every zip code in your target country along with its corresponding Latitude and Longitude. Free databases for US Zip Codes are widely available online in CSV format. Import this into a sheet named ZipReference and convert it into an Excel Table named tblZipRef with three core columns: Zip, Latitude, and Longitude.
Set up a small control panel in an empty sheet or at the top of your workspace. This is where you will input your target parameters:
90210)25)Before we can calculate distances, we need Excel to look up the latitude and longitude of the target zip code (from cell H2) and each customer's zip code in our database. We will use the modern XLOOKUP function for this.
To find the Latitude of our target zip code (H2), we use:
=XLOOKUP(H2, tblZipRef[Zip], tblZipRef[Latitude], "Not Found")
To find the Longitude of our target zip code, we use:
=XLOOKUP(H2, tblZipRef[Zip], tblZipRef[Longitude], "Not Found")
Now, we will add a new column to our customer database (tblCustomers) called Distance (Miles). Excel's trigonometric functions (SIN, COS, ACOS) work in radians, not degrees. Therefore, we must wrap every latitude and longitude degree value in the RADIANS() function.
Assuming the customer's Zip Code is in cell E5, the target zip code is in $H$2, and your lookup formulas are nested, the complete Excel formula for the distance column looks like this:
=LET(
target_lat, XLOOKUP($H$2, tblZipRef[Zip], tblZipRef[Latitude]),
target_lon, XLOOKUP($H$2, tblZipRef[Zip], tblZipRef[Longitude]),
cust_lat, XLOOKUP(E5, tblZipRef[Zip], tblZipRef[Latitude]),
cust_lon, XLOOKUP(E5, tblZipRef[Zip], tblZipRef[Longitude]),
earth_radius, 3959,
IF(OR(ISNA(cust_lat), ISNA(target_lat)), "Unknown Zip",
ACOS(
SIN(RADIANS(target_lat)) * SIN(RADIANS(cust_lat)) +
COS(RADIANS(target_lat)) * COS(RADIANS(cust_lat)) *
COS(RADIANS(cust_lon) - RADIANS(target_lon))
) * earth_radius
)
)
target_lat and cust_lat). This prevents Excel from performing the same duplicate XLOOKUP queries multiple times, drastically increasing calculation speeds on large databases.#N/A or #VALUE! error.Now that your database calculates the distance to the target zip code dynamically for every row, you can filter this data using two different methods depending on your version of Excel.
$H$3).If you are using modern Excel, you can keep your master database clean and generate an auto-updating, filtered list on a separate sheet using the FILTER function.
On your dashboard/reporting sheet, write the following formula:
=FILTER(tblCustomers, (tblCustomers[Distance (Miles)] <= H3) * ISNUMBER(tblCustomers[Distance (Miles)]), "No Locations Found")
This dynamic formula checks the Distance column in your customer table, compares it to your maximum radius input in cell H3, and instantly spills a list of all matching rows. If you change the Target Zip Code in H2 or the radius in H3, the entire filtered table will instantly recalculate and redraw itself.
If you are working with address databases containing 50,000+ rows, heavy trigonometric formulas combined with nested lookups can cause Excel to lag. Use these optimization tips to keep your workbook lightning-fast:
XLOOKUP inside the distance formula for every customer row. Instead, create static Latitude and Longitude columns directly in your customer database and pull the coordinates once. Then, refer to those static cells in your distance formula.By leveraging basic trigonometry and modern lookup functions, you can turn Microsoft Excel into a powerful geographic analysis engine. This radius-filtering tool allows you to target marketing efforts, streamline logistics paths, and make data-driven territorial decisions without ever leaving your spreadsheet.
Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.