Calculate the distance between 2 postcodes in miles

Jaffabfc

Board Regular
Joined
Jul 5, 2013
Messages
230
Office Version
  1. 365
Platform
  1. Windows
Hi,

i am wanting to be able to calculate the distance between 2 postcodes in excel.

i have a list of around 3000 places in the UK and i am wanting to know which is the closest to were i currently am.

To do this i am wanting to be able to put a column on the end (e.g. column f) to work out how much it is from A to B, is this even possible?

I am hoping there is something simple that i can do even if it means linking google maps into it.

Thanks
 
I've added the Microsoft XML v6.0 reference in VBA but i'm getting #NAME? when I try to use =GetDistance("T2P 4R5", "M9N 1K9")
Look at my post two posts above yours for possible causes of the error and you don't need the reference.
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Look at my post two posts above yours for possible causes of the error and you don't need the reference.
Thank you for your help

I've got it working now. One last question, is their anyway to manual run the lookup? i.e if I make changes to the postcodes to reference it doesn't perform a lookup
 
Upvote 0
anyway to manual run the lookup? i.e if I make changes to the postcodes to reference it doesn't perform a lookup
What do you mean by manually run the lookup?

If you reference cells containing the start and end postcodes in the formula, instead of hardcoding them, then it will recalculate the formula when you change any of the postcodes, for example:
Excel Formula:
=GetDistance(A1,B1)
 
Upvote 0
@John_w Thanks for your help with this script. Its working great.


Is their any way I change things so once it has looked up the distance it doesn't do it again? My logistics spreadsheet at any time has around 200 postcode locations so when I open the sheet up it resolves every postcode which takes ages

Thanks
 
Upvote 0
Is their any way I change things so once it has looked up the distance it doesn't do it again? My logistics spreadsheet at any time has around 200 postcode locations so when I open the sheet up it resolves every postcode which takes ages
Delete the =GetDistance cell formulas and instead run this macro, which loops through the rows in the active sheet starting at A2 and reads the start postcode from column A and the end postcode from column B and puts the distance in miles in column C.

VBA Code:
Public Sub Calculate_Distances()

    Dim cell As Range
    
    With ActiveSheet
        For Each cell In .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
            cell.Offset(, 2).Value = GetDistance(cell.Value, cell.Offset(, 1).Value)
        Next
    End With
    
End Sub
 
Upvote 0
Delete the =GetDistance cell formulas and instead run this macro, which loops through the rows in the active sheet starting at A2 and reads the start postcode from column A and the end postcode from column B and puts the distance in miles in column C.

VBA Code:
Public Sub Calculate_Distances()

    Dim cell As Range
   
    With ActiveSheet
        For Each cell In .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
            cell.Offset(, 2).Value = GetDistance(cell.Value, cell.Offset(, 1).Value)
        Next
    End With
   
End Sub

That's great, can you explain which part of this code I would need to alter so I can hard code the end postcode (Currently column B)?
 
Upvote 0
That's great, can you explain which part of this code I would need to alter so I can hard code the end postcode (Currently column B)?
The 2nd argument to GetDistance is the end postcode, therefore:

VBA Code:
           cell.Offset(, 2).Value = GetDistance(cell.Value, "end postcode")
 
Upvote 0

Forum statistics

Threads
1,224,833
Messages
6,181,237
Members
453,026
Latest member
cknader

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top