Adrian
On Tuesday, October 27, 2009 12:12 PM joel wrote:
Are you using any website. Can you provide a sample of the 2 postcodes
you are using for an example. I am in the US and don;t the code and
would have to do a little research.
What can be done is using google get the map of the distance between
the two locations and extract the distance from the returned webpage..
--
joel
------------------------------------------------------------------------
joel's Profile:
http://www.thecodecage.com/forumz/member.php?userid=229
View this thread:
http://www.thecodecage.com/forumz/showthread.php?t=148279
On Thursday, October 29, 2009 10:11 AM Adrian wrote:
Joel,
Here is the sample code that I was lookng at, can this be modified towork
down a sheet where the starting postcodes are in column A and the finish
postcodes are in column B placing the mileage result in column C
Private Sub CommandButton1_Click()
counter = 6
beginrange = Worksheets("sheet1").Cells(counter, 4).Address
endrange = Worksheets("sheet1").Cells(counter, 14).Address
For Each c In Worksheets("Sheet1").Range(beginrange, endrange).Cells
If c.Offset(0, 1).Value = "" Then counter = counter + 2
If counter = 20 Then Exit Sub
beginrange = Worksheets("sheet1").Cells(counter, 4).Address
endrange = Worksheets("sheet1").Cells(counter, 14).Address
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL =
"
http://www.postcode.org.uk/country/uk/_postcode-distance-calculator.asp"
IE.Navigate2 URL
Do While IE.readyState <> 4
DoEvents
Loop
Do While IE.busy = True
DoEvents
Loop
Set Form = IE.document.getElementsByTagname("Form")
Set inputform = Form.Item(0)
Set Postcodebox = inputform.Item(0)
Postcodebox.Value = c.Value
Set Postcodebox2 = inputform.Item(1)
Postcodebox2.Value = c.Offset(0, 1).Value
Set POSTCODEbutton = inputform.Item(2)
POSTCODEbutton.Click
Do While IE.busy = True
Loop
Set Table = IE.document.getElementsByTagname("Table")
Set DistanceTable = Table.Item(3)
Set DistanceRow = DistanceTable.Rows(2)
distance = Val(Trim(DistanceRow.Cells(2).innertext))
c.Offset(1, 1).Value = distance
IE.Quit
Next
End Sub
So the sheet would look like this before the macro ran and would have
mileages in column C when finished.
A B C D
1 CF83 4ES CM2 5PX
2 GL3 4PU DN21 1LG
3
4
--
Adrian
:
On Thursday, October 29, 2009 1:24 PM joel wrote:
I did something similar Using google the other day when you posted the
code. The code you provide was using AS THE CROW FLY I changed itto
Driving distance and added the required loop. I also made some changes
to make it easier to understand.
Private Sub CommandButton1_Click()
RowCount = 6
FirstCol = "D"
LastCol = "N"
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
--
joel
------------------------------------------------------------------------
joel's Profile:
http://www.thecodecage.com/forumz/member.php?userid=229
View this thread:
http://www.thecodecage.com/forumz/showthread.php?t=148279
On Monday, November 02, 2009 9:25 AM Adrian wrote:
Thanks Joel but maybe I am doing something wrong, I have copied thecode into
a new module in a new book and placed the starting postcodes into column A
starting at row 6 and finishing postcodes into column B starting atrow 6.
The macro runs and the website opens but it does not return the result in
cell C6 as expected. Have I done something wrong ?
--
Adrian
:
On Monday, November 02, 2009 9:37 AM joel wrote:
I was following your original code. The start locations was in D6 and
end Location in E6. Then each start and end location where two columns
to the right. Each results was put in E7 and moved to the rightby two
columns ] one row down and one row to the right .offset(1,1)].
Is the Start and End Locations being put in to the website in the
correct boexes? I want to find out if the problem with the input data
or the output data.
--
joel
------------------------------------------------------------------------
joel's Profile:
http://www.thecodecage.com/forumz/member.php?userid=229
View this thread:
http://www.thecodecage.com/forumz/showthread.php?t=148279
<a href="
http://www.thecodecage.com">MicrosoftOffice Help</a>
On Wednesday, December 16, 2009 2:21 PM ant_west wrote:
Joel - thanks, that is brilliant and just what I needed!
I changed your macro slightly for my needs - in particular I changed
the line
ColCount = ColCount + 2
to
RowCount = RowCount + 1
as I was going down a column. (I was also calculating distances from
one fixed point)
Submitted via EggHeadCafe - Software Developer Portal of Choice
Dynamic Data Controls with Entity Framework
http://www.eggheadcafe.com/tutorials/aspnet/29c02d78-c90d-495a-82fd-c...- Hide quoted text -