VBA Excel MapPoint - Reset Value in a ForEach Loop

Hello,

I need the value for objRoute.distance to be reset after every ForEach loop. (Because I need each route distance as a separate value.) Setting objRoute.distance equal to zero doesn't work. Please help?

Thanks,
David



Sub Button2_Click()
  
  Dim objApp As Object
  Set objApp = CreateObject("MapPoint.Application")
  Dim objCell As Range

 
' Set up application

  Set objApp = CreateObject("MapPoint.Application")
  Set objMap = objApp.ActiveMap
  Set objRoute = objMap.ActiveRoute
 
  objApp.Visible = True
  objApp.UserControl = True

' Add route stops and calculate the route
 
  For Each objCell In Range("E6", Range("E6").End(xlDown))
 

      objRoute.Waypoints.Add objMap.FindResults(objCell).Item(1)
      objRoute.Waypoints.Add objMap.FindResults(objCell.Offset(0, 1)).Item(1)
     
      objRoute.Calculate
      objCell.Offset(0, 3) = objRoute.distance
      objRoute.distance = 0
     
  Next objCell

End Sub

Open in new window

d-cdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

telyni19Commented:
I haven't used MapPoint specifically, but I know how both VBA and GPS units work in general. It looks like the Route.Distance property is an automatic property of the entire route. If you want to get the difference in distance after you add each set of waypoints to the route, the easiest way would seem to be just to keep track of the previous distance and calculate the difference each time. See the modified code attached. Does this do what you intended?
Sub Button2_Click()
  
  Dim objApp As Object
  Set objApp = CreateObject("MapPoint.Application")
  Dim objCell As Range
  Dim prevdist As Long

' Set up application

  Set objApp = CreateObject("MapPoint.Application")
  Set objMap = objApp.ActiveMap
  Set objRoute = objMap.ActiveRoute
 
  objApp.Visible = True
  objApp.UserControl = True

' Add route stops and calculate the route
  prevdist = 0
  For Each objCell In Range("E6", Range("E6").End(xlDown))
      objRoute.Waypoints.Add objMap.FindResults(objCell).Item(1)
      objRoute.Waypoints.Add objMap.FindResults(objCell.Offset(0, 1)).Item(1)
      objRoute.Calculate
      objCell.Offset(0, 3) = objRoute.Distance - prevdist
      prevdist = objRoute.Distance
  Next objCell
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
d-cdAuthor Commented:
Thank you! That worked perfectly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.