View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_3_] Bob Phillips[_3_] is offline
external usenet poster
 
Posts: 2,420
Default sub for calculating distance via post code / variable range.


By the time that you up counter and use that to redefine beginrange and
endrange, you are already in the cell processing loop, so it makes no
difference.

Just change the code to do nothing if that cell is empty and pass onto the
next cell in the specified range.

--
__________________________________
HTH

Bob

"Atishoo" wrote in message
...
Hi im using the sub below to calculate a mileage sheet populated with post
codes.
I am using the variable "counter" to set the row that the sub applies to.
So in teory it should calculate the distances for each post code in row 6
(counter initial value) and when it comes to an empty cell in row 6 (if
c.value offset (0, 1)="" then counter = counter +2) it should move onto
row 8
and so on until hitting row 20 (if counter = 20 exit sub).

But Im missing something when it hits an empty cell it just keeps going on
row 6!
What am i doing wrong here??




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


many thanks