Link to home
Start Free TrialLog in
Avatar of d-cd
d-cd

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of telyni19
telyni19
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of d-cd
d-cd

ASKER

Thank you! That worked perfectly.