Nearest Postcode Search in Microsoft Excel

I need to find a load of addresses by proximity to a postcode. I have the addresses in a Microsoft Access database.

  1. Download Paul Jenkins’s UK Postcode csv and import into Access as a table
  2. Create a query on your own table adding the following calculated field:
    <pre>PCregion: Trim( Left( [My Table]![Postcode], InStr( [My Table]![Postcode], ” ” ) ) )</pre>
  3. Create a second query that joins the above query to the imported postcode table. Join on the “PCregion” field created above. Also add in the latitude and longitude fields from the postcodes table

Well, I couldn’t be bothered to fight with Access so I exported the key tables and used Excel instead!

The principals are the same. Load the tables as tabs, load the Postcode data as a tab. Create a front sheet containing the reference Postcode (Create a Named Cell for this “STARTREGION”). Translate the reference Postcode to Latitude and Longitude by doing a VLOOKUP to the Postcode table, e.g.:

=VLOOKUP(TRIM(LEFT(RC2,SEARCH(" ",RC2 & " "))),UK_PostCodes!C1:C4,3,FALSE)

Where RC2 contains the reference Postcode and “UK_PostCodes!C1:C4” is the postcodes table (column 3 contains the Latitude and 4 the Longitude). Note that I’ve used R1C1 reference style as it is easier to mix fixed and relative references.

Now use the same formula in the main data tables so that you have Lat. and Long. columns added based on the Postcode on each line.

Finally add a “Distance” column to each data table with the following formula:

=ROUND(DEGREES(ACOS(SIN(RADIANS(STARTLAT))*SIN(RADIANS(RC28))+COS(RADIANS(STARTLAT))*COS(RADIANS(RC28))*COS(RADIANS(STARTLON-RC29))))*60*1.1515,0)

In this case, we’ve used the STARTREGION named cell and the Lat./Long. data is in columns 28 & 29 respectively.

The calculation and Postcode reference data came from HM2K’s website: How to search by nearest uk postcode in php.