Hi guys,
I am creating a function to convert a position from DMS (Degree, Minute, Second) format to DD (Decimal Degree) format.
I couldn't find anything good on the web, so I'm now making my own version.
The function below is working fine, except for the fact that the DMS parameter normally contains an " sign as the Second indicator.
For example: 5° 40' 23" E
I am now removing this sign manually from the parameter, but is it possible to adjust the function so that it threats the " sign literally?
So currently:
?FormatDD("5° 40' 23 E") works and returns +05.6730556
?FormatDD("5° 40' 23" E") Doesnt work, since the function see's a second parameter...
Any idea's?
I am creating a function to convert a position from DMS (Degree, Minute, Second) format to DD (Decimal Degree) format.
I couldn't find anything good on the web, so I'm now making my own version.
The function below is working fine, except for the fact that the DMS parameter normally contains an " sign as the Second indicator.
For example: 5° 40' 23" E
I am now removing this sign manually from the parameter, but is it possible to adjust the function so that it threats the " sign literally?
Code:
Function FormatDD(DMS As String) As String
Dim Part() As String
Dim strPosNeg As String
Dim strDecimal As String
DMS = Replace(DMS, Chr(176), "") 'Remove degree sign (°)
DMS = Replace(DMS, Chr(39), "") 'Remove minute sign (')
DMS = Replace(DMS, Chr(34), "") 'Remove second sign (")
Part = Split(DMS, Chr(32))
'Determine leading positive or negative sign
strPosNeg = IIf(Left(Part(0), 1) = "-", "-", "+")
'Convert DMS to DD
strDecimal = IIf(InStr("SW", Part(3)), -1, 1) * (Val(Part(0)) + Val(Part(1)) / 60 + Val(Part(2)) / 3600)
'Remove any leading or trailing spaces
strDecimal = Trim(strDecimal)
'Limit the result to a maximum of 7 decimals
strDecimal = Round(strDecimal, 7)
'Replace the comma to a period in the result
strDecimal = Replace(strDecimal, Chr(44), Chr(46))
'Add a leading zero in
strDecimal = IIf(Len(Split(strDecimal, Chr(46))(0)) = 1, "0" & strDecimal, strDecimal)
'Return result
FormatDD = strPosNeg & strDecimal
End Function
So currently:
?FormatDD("5° 40' 23 E") works and returns +05.6730556
?FormatDD("5° 40' 23" E") Doesnt work, since the function see's a second parameter...
Any idea's?