iwishiwasgoodatvba
New Member
- Joined
- Nov 9, 2020
- Messages
- 1
- Office Version
- 2019
- 2016
- Platform
- Windows
Hello,
I have a list of 45 geographical coordinates. Using all these 45 places, I need to find the best path to take. The starting place of the geographical location can vary. You may start off with the 5th place or you can start off with the 37th place. From whatever the place is that you start off with, I need to determine what place I go to next, and then what the next place is that I go to from the previous place until I reach my last place.
I understand this code below, but it just gives distance from 2 places whereas I need to find the distance from the first place I choose, then all the others until I get a list of place 1-45 to take.
I have a list of 45 geographical coordinates. Using all these 45 places, I need to find the best path to take. The starting place of the geographical location can vary. You may start off with the 5th place or you can start off with the 37th place. From whatever the place is that you start off with, I need to determine what place I go to next, and then what the next place is that I go to from the previous place until I reach my last place.
I understand this code below, but it just gives distance from 2 places whereas I need to find the distance from the first place I choose, then all the others until I get a list of place 1-45 to take.
VBA Code:
Public Function GetDistanceCoord(ByVal lat1 As Double, ByVal lon1 As Double, ByVal lat2 As Double, ByVal lon2 As Double, ByVal unit As String) As Double
Dim theta As Double: theta = lon1 - lon2
Dim dist As Double: dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta))
dist = WorksheetFunction.Acos(dist)
dist = rad2deg(dist)
dist = dist * 60 * 1.1515
If unit = "K" Then
dist = dist * 1.609344
ElseIf unit = "N" Then
dist = dist * 0.8684
End If
GetDistanceCoord= dist
End Function
Function deg2rad(ByVal deg As Double) As Double
deg2rad = (deg * WorksheetFunction.Pi / 180#)
End Function
Function rad2deg(ByVal rad As Double) As Double
rad2deg = rad / WorksheetFunction.Pi * 180#
End Function
Last edited by a moderator: