[/CODE]
Private Const C_PI As Double = 3.14159265358979 Function GreatCircleDistance(Latitude1 As Double, Longitude1 As Double, _ Latitude2 As Double, Longitude2 As Double) As Double Dim Lat1 As Double Dim Lat2 As Double Dim Long1 As Double Dim Long2 As Double Dim X As Long Dim Delta As Double X = 24 ' convert to decimal degrees Lat1 = Latitude1 * X Long1 = Longitude1 * X Lat2 = Latitude2 * X Long2 = Longitude2 * X ' convert to radians: radians = (degrees/180) * PI Lat1 = (Lat1 / 180) * C_PI Lat2 = (Lat2 / 180) * C_PI Long1 = (Long1 / 180) * C_PI Long2 = (Long2 / 180) * C_PI ' get the central spherical angle Delta = ((2 * ArcSin(Sqr((Sin((Lat1 - Lat2) / 2) ^ 2) + _ Cos(Lat1) * Cos(Lat2) * (Sin((Long1 - Long2) / 2) ^ 2))))) GreatCircleDistance = Delta * C_RADIUS_EARTH_KM End Function Function ArcSin(X As Double) As Double ' VBA doesn't have an ArcSin function. Improvise ArcSin = Atn(X / Sqr(-X * X + 1)) End Function Sub Checking() Dim Lat1 As Double Dim Long1 As Double Dim Lat2 As Double Dim Long2 As Double Dim results As Double results = GreatCircleDistance(Lat1, Long1, Lat2, Long2) Do While Not IsEmpty(Cells(4, 1)) Lat1 = Cells(4, 1) Long1 = Cells(4, 2) Lat2 = Cells(4, 3) Long2 = Cells(4, 4) Cells(4, 5) = results Loop End Sub
Private Const C_PI As Double = 3.14159265358979 Function GreatCircleDistance(Latitude1 As Double, Longitude1 As Double, _ Latitude2 As Double, Longitude2 As Double) As Double Dim Lat1 As Double Dim Lat2 As Double Dim Long1 As Double Dim Long2 As Double Dim X As Long Dim Delta As Double X = 24 ' convert to decimal degrees Lat1 = Latitude1 * X Long1 = Longitude1 * X Lat2 = Latitude2 * X Long2 = Longitude2 * X ' convert to radians: radians = (degrees/180) * PI Lat1 = (Lat1 / 180) * C_PI Lat2 = (Lat2 / 180) * C_PI Long1 = (Long1 / 180) * C_PI Long2 = (Long2 / 180) * C_PI ' get the central spherical angle Delta = ((2 * ArcSin(Sqr((Sin((Lat1 - Lat2) / 2) ^ 2) + _ Cos(Lat1) * Cos(Lat2) * (Sin((Long1 - Long2) / 2) ^ 2))))) GreatCircleDistance = Delta * C_RADIUS_EARTH_KM End Function Function ArcSin(X As Double) As Double ' VBA doesn't have an ArcSin function. Improvise ArcSin = Atn(X / Sqr(-X * X + 1)) End Function Sub Checking() Dim Lat1 As Double Dim Long1 As Double Dim Lat2 As Double Dim Long2 As Double Dim results As Double results = GreatCircleDistance(Lat1, Long1, Lat2, Long2) Do While Not IsEmpty(Cells(4, 1)) Lat1 = Cells(4, 1) Long1 = Cells(4, 2) Lat2 = Cells(4, 3) Long2 = Cells(4, 4) Cells(4, 5) = results Loop End Sub
Code:
[COLOR=#333333]Yes, it is not really my own written coding and have been copied from the [/COLOR][COLOR=#333333]([/COLOR][URL="http://www.cpearson.com/excel/latlong.aspx"]Latitude And Longitude[/URL][COLOR=#333333])[/COLOR][COLOR=#333333]and with some modification which by deleting the miles option because I want it to be calculate straightaway to Km and i've deleted ValueAsDecimaldegrees part because my latitude and longitude data in deg:min:sec form.[/COLOR]
[COLOR=#333333]From what I understand, the function cannot stand alone, it have to be put inside the sub or called from the sub. Is it right? The input is the latitude and longitude and i want the function to calculate the distance between the coordinates.[/COLOR]
[COLOR=#333333]Actually, i'm new with vb and do not really know to do programming. I'm just do some reading, try out some examples and finding coding that quite related to what i'm going to do and modify it to what i understand.[/COLOR]