Solved

adapt excel postcode distance calculator

Posted on 2011-03-01
4
964 Views
Last Modified: 2012-06-27
I'm trying to adapt code I've found to my needs and it's working as I'd expect pretty much. However, there's an issue I need to fix I hope you guys can help with.

Basically, I have engineers travelling from one place to another, frequently via a middle destination, but not always. I have a script that looks up the postcode distances and this works just fine for valid postcodes, but I can't work out how to put logic in the code to make it work for when the engineer travels without going to the middle destination.

To explain, look at engineer1 screenshot.

As you can see, engineers normally go from home to tote, to job. However, they MAY go direct from tote to job, or direct from home to job. I need the code to have enough logic to compute this somehow.

The code I'm using currently does the "home to tote" column calculation, and then does the "tote to job" calculation, and this works well. However obviously the "n/a"'s cause a problem and reflect 0 miles.

I'm hoping someone can assist me :)

Thanks!

My current code is attached.

Ross
Private Sub GetDistances_Click()

RowCount = 2
FirstCol = "C"
LastCol = "N"
ColCount = Columns(FirstCol).Column

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False

URL = "http://www.postcode.org.uk/country/uk/_postcode-distance-calculator.asp"

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(5).innertext))

Cells(RowCount + 0, ColCount + 7) = distance

RowCount = RowCount + 1
Loop
End With

RowCount = 2
FirstCol = "D"
LastCol = "N"
ColCount = Columns(FirstCol).Column

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False

URL = "http://www.postcode.org.uk/country/uk/_postcode-distance-calculator.asp"

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(5).innertext))

Cells(RowCount + 0, ColCount + 7) = distance

RowCount = RowCount + 1
'ColCount = ColCount + 1
Loop
End With

IE.Quit

End Sub

Open in new window

engineer1.JPG
0
Comment
Question by:RossDagley1
  • 3
4 Comments
 
LVL 24

Accepted Solution

by:
StephenJR earned 500 total points
ID: 35008331
Not tested, but does changing these two lines work?
StartLocation = IIf(.Cells(RowCount, ColCount).Value = "N/A", .Cells(RowCount, ColCount + 1).Value, .Cells(RowCount, ColCount).Value)
        EndLocation = IIf(.Cells(RowCount, ColCount + 1).Value = "N/A", .Cells(RowCount, ColCount + 2).Value, .Cells(RowCount, ColCount + 1).Value)

Open in new window

0
 
LVL 24

Expert Comment

by:StephenJR
ID: 35008422
Actually not sure on reflection whether that change will do the trick. Could you post a small workbook please?
0
 
LVL 2

Author Closing Comment

by:RossDagley1
ID: 35008469
Absolutely spot on, and exactly what I was looking for. Solved the issue promptly and accurately. thank you very much - saved me a LOT of head scratching!
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 35008508
Ok, glad it worked! A pleasant surprise.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now