VBA to calculate distance between Uk Postcodes - Urgent help

hoppy69efc

New Member
Joined
Dec 7, 2011
Messages
6
Hi all,

Iam not the best when its comes to Macro's and VBA's as I am only just starting to learn the stuff
confused.gif


Can anyone help me create a Macro to determine the distance between two UK Postcodes? For Example a1: L21 1LA b1: L3 9SJ c1: 5.1 miles

Any help and some sort of idiots guide would be greatly appreciated,

Thanks in advance!

Jason​
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Thanks, although I could do with not having to go off Lon/lat and just off standard Uk post codes.....been trying to use the below Macro but its returning values of 0?

Public Function getGoogDistanceTime(rngSAdd As Range, rngEAdd As Range, Optional strReturn As String = "distance") As Variant
Dim sURL As String
Dim BodyTxt As String
Dim vUnits As Variant
Dim dblTemp As Double
Dim bUnit As Byte
sURL = "http://maps.google.co.uk/maps?hl=en&tab=wl"
sURL = sURL & "&saddr=" & Replace(rngSAdd(1).Value, " ", "+")
sURL = sURL & "&daddr=" & Replace(rngEAdd(1).Value, " ", "+")
sURL = sURL & "&hl=en"
BodyTxt = getHTML(sURL)
If InStr(1, BodyTxt, strReturn, vbTextCompare) = 0 Then
getGoogDistanceTime = "Error"
Else
getGoogDistanceTime = parseGoog(strReturn, BodyTxt)
If LCase(strReturn) Like "time*" Then
vUnits = Split(getGoogDistanceTime)
For bUnit = LBound(vUnits) To UBound(vUnits) - 1 Step 2
dblTemp = dblTemp + _
Val(vUnits(bUnit)) / Choose(InStr(1, "hms", Left(vUnits(bUnit + 1), 1), vbTextCompare), 24, 1440, 86400)
Next bUnit
getGoogDistanceTime = dblTemp
Else
getGoogDistanceTime = Val(getGoogDistanceTime)
End If
End If
End Function
Public Function getHTML(strURL As String) As String
Dim oXH As Object
Set oXH = CreateObject("msxml2.xmlhttp")
With oXH
.Open "get", strURL, False
.send
getHTML = .responseText
End With
Set oXH = Nothing
End Function
Public Function parseGoog(strSearch As String, strHTML As String) As String
strSearch = strSearch & ":'"
If InStr(1, strHTML, strSearch) = 0 Then
parseGoog = "Not Found"
Exit Function
Else
parseGoog = Mid(strHTML, InStr(1, strHTML, strSearch) + Len(strSearch))
parseGoog = Mid(parseGoog, 1, InStr(1, parseGoog, """") - 1)
End If
End Function
 
Upvote 0
Thanks that works perfectly but I have around 1500 to do so not sure how I could work that in, I have the attached working macro but need it so it puts the miles in C6 and works all the way down the column, any ideas? Helps much appreciated!

Jason
 
Upvote 0
Sorry forgot to attach but not sure how to do it!! Code I am using is below

Private Sub CommandButton1_Click()
RowCount = 6
FirstCol = "A"
LastCol = "C"
ColCount = Columns(FirstCol).Column
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "http://www.postcode.org.uk/country/uk/" & _
"_postcode-distance-calculator.asp"
'get to first webpage
IE.Navigate2 URL
Do While IE.readyState <> 4 Or _
IE.busy = True
DoEvents
Loop

With Worksheets("Sheet1")
Do While .Cells(RowCount, ColCount) <> ""
StartLocation = .Cells(RowCount, ColCount).Value
EndLocation = .Cells(RowCount, ColCount + 1).Value
Set Form = IE.document.getElementsByTagname("Form")
Set inputform = Form.Item(0)
Set Postcodebox = inputform.Item(0)
Postcodebox.Value = StartLocation
Set Postcodebox2 = inputform.Item(1)
Postcodebox2.Value = EndLocation
Set POSTCODEbutton = inputform.Item(2)
POSTCODEbutton.Click
Do While IE.readyState <> 4 Or _
IE.busy = True
DoEvents
Loop
Set Table = IE.document.getElementsByTagname("Table")
Set DistanceTable = Table.Item(3)
Set DistanceRow = DistanceTable.Rows(2)
distance = Val(Trim(DistanceRow.Cells(4).innertext))
.Cells(RowCount + 1, ColCount + 1) = distance
ColCount = ColCount + 2
Loop
End With

IE.Quit
End Sub
 
Upvote 0
Dear All

I have been trying to get the code as shown by Hoppy69 in his 22 December post to work, but with no luck at all, not the most advanced user by any means, I also get the 0 return.

Using 2010, and any help would be greatly appreciated


Thanks in advance

A
 
Upvote 0
Hi, Tried this code , however the link seem to have an issue. tried with a different link but didnt work "Runtime error 91"
Code tried
Sub Swaroop()


RowCount = 6
FirstCol = "I"
LastCol = "J"
ColCount = Columns(FirstCol).Column


Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "http://www.freemaptools.com/how-far-is-it-between.htm"


'get to first webpage
IE.Navigate2 URL
Do While IE.readyState <> 4 Or _
IE.busy = True


DoEvents
Loop




With Worksheets("Analysis sheet")
Do While .Cells(RowCount, ColCount) <> ""
StartLocation = .Cells(RowCount, ColCount).Value
EndLocation = .Cells(RowCount, ColCount + 1).Value


Set Form = IE.document.getElementsByTagname("Form")
Set inputform = Form.Item(0)
Set frombox = inputform.Item(0)
frombox.Value = StartLocation


Set tobox = inputform.Item(1)
tobox.Value = EndLocation


Set Showbutton = inputform.Item(2)
Showbutton.Click
Do While IE.readyState <> 4 Or _
IE.busy = True


DoEvents
Loop


Set Table = IE.document.getElementsByTagname("DistancebyLandTransport")
Set DistanceTable = Table.Item(2)


Set DistanceRow = DistanceTable.Rows(7)
distance = Val(Trim(DistanceRow.Cells(3).innertext))


.Cells(RowCount, ColCount + 2) = distance


RowCount = RowCount + 1
Loop


End With


IE.Quit


End Sub

Could you help me set this right
 
Upvote 0
Pm your email address and I will send you my xls that can calculate the distances. You will just need to drop your Postcodes into a Column and then simply copy and paste the calculation formula to give you what you are looking to do.
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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