Closest Zip Code Analysis using Microsoft Excel and CDXZipStream

Microsoft Excel "custom functions" can do many specialized tasks related to address and location-based information, such as checking the validity of mailing addresses, importing demographic information, even calculating driving time and driving distance. Custom functions, also referred to as UDFs (user-defined functions) perform complex calculations or tasks and are used in cell formulas just like the standard Excel functions SUM or AVERAGE. In this article we will describe how to use custom functions to identify which zip code within a list is closest to a target location.

Identification of the closest zip code or address has many applications. For example, a mailing list of potential customers can be matched with the closest store locations, or a list of delivery addresses can be correlated with the closest distribution centers. These kinds of analyses require geocode (latitude and longitude) coordinates in order to calculate the straight-line "as the crow flies" distance between locations. After these distance calculations are completed for all possible combinations of zip codes, the one with the shortest distance is selected.

CDXZipStream is a Microsoft Excel add-in that is capable of doing all this and more. A custom function named CDXClosestZip can automatically grab latitude and longitude data from a local database, and then perform distance calculations based on this data. Since all this occurs completely in the background, there's no need to learn a new application or even leave the familiar environment of Excel. For example, let's say we have a list of store addresses in the worksheet range A1 through A10. We want to find the zip code of a store closest to a customer in zip 08034 by inputting the custom function into a worksheet cell like this: "= CDXClosestZip ("08034", 0, A1:A10)". The result returned to the worksheet will be the closest store within the specified range A1:A10. Instead of entering "0" for the middle parameter other variables can be entered to return the second, third closest zips or distances involved with the returned zip codes.

CDXZipStream also has a "right-click" function to lead you thru entering the parameters of the CDXClosestZip function. Here is the dialog that appears

CDXZipStream Closest Zip Dialog data entry

We can apply the same custom function to multiple customer addresses listed in column B. If the first customer zip code is in worksheet cell B1, the formula in cell C1 would be = CDXClosestZip(B1,0,A$1:A$10). We can then simply copy and paste this formula all along the rest of the list to find the closest store for each customer. The $ is used to prevent the formula from referencing a different set of cells. You can substitute a range name instead as a way to prevent this or reference cells on a different sheet.

This is a good example of how Excel custom functions can automate various tasks and calculations, with no need to learn a new application or even leave the familiar environment of Excel. From checking the validity of address information to finding the closest zip code to a target location, custom functions can be valuable tools for analyzing address and location-based information.

The video below shows how this all works:

Find Closest Zip Code in Excel

We've developed free templates to make this analysis even easier. A closest zip template is available for download at the link Check out the following video to see how it works.

Closest ZIP Code Template

CDXZipStream is an Excel add-in that provides zip code data, demographics by zip code, route optimization, zip code radius analysis, geocoding, and mapping using custom functions. A free, 30-day trial version is available for download from the website, as well as example spreadsheets and tutorials showing how to use Excel custom functions in a variety of applications.