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.
How can this be accomplished within an Excel worksheet? A custom function 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 can find the store closest to a customer in zip 08034 by inputting the custom function into a worksheet cell like this: “= CustomFunction (A1:A10, 08034)”. The result returned to the worksheet will be the closest store within the specified range A1:A10.
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 = CustomFunction (A1:A10, B1). We can then simply copy and paste this formula all along the rest of the list to find the closest store for each customer.
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.