Hi all
Here is a a macro I wrote for excel 15 years ago. I don't remember how I did it or how to write code and there's a problem with it.
It converts decimal degrees to Degrees Minutes Seconds
example: it will convert 150.123456789 to 150°7'54"
The problem is when there are "1"s at certain places the minutes value gets messed up
example 0.00111 will return 0°6'0.4" when it should return 0°0'0.4"
Any help is appreciated
Thanks
Here is a a macro I wrote for excel 15 years ago. I don't remember how I did it or how to write code and there's a problem with it.
It converts decimal degrees to Degrees Minutes Seconds
example: it will convert 150.123456789 to 150°7'54"
The problem is when there are "1"s at certain places the minutes value gets messed up
example 0.00111 will return 0°6'0.4" when it should return 0°0'0.4"
Any help is appreciated
Thanks
VBA Code:
'By Nathan Converts decimal degrees to dd mm'ss"
Public Function dd2dms(dec As Variant) As Variant
If Not IsNumeric(dec) Then
dd2dms = dec
Exit Function
End If
Dim data As String
data = CStr(dec)
Dim strtemp As String
Dim intPos As Integer
Dim min As String
Dim deg As String
Dim sec As String
intPos = InStr(data, ".")
If (intPos = 0) Then
dd2dms = data & Chr(176) & "00" & Chr(39) & "00" & Chr(34)
Exit Function
End If
deg = Left(data, intPos - 1)
strtemp = Mid(data, intPos + 1)
If (CSng(strtemp) < 0.0000001) Then
dd2dms = deg & Chr(176) & "00" & Chr(39) & "00" & Chr(34)
Exit Function
End If
Dim seconds As Single
''seconds = CSng("0." & strtemp) * 3600
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''Get Minutes
min = CStr(CSng("0." & strtemp) * 60)
intPos = InStr(min, ".")
If (intPos > 0) Then
seconds = CSng("0." & Mid(min, intPos + 1))
min = Left(min, intPos - 1)
Else
seconds = 0#
End If
'below changed by nathan
'the last number "00.0" controls decimal places shown
'change to whatever you dig
'if you dont want any decimals change to "00" and so on
seconds = Format(seconds * 60, "00.0")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Get Seconds
If Len(CStr(seconds)) < 2 Then
sec = Right("0" & CStr(seconds), 2)
Else
sec = CStr(seconds)
End If
dd2dms = deg & Chr(176) & min & Chr(39) & sec & Chr(34)
End Function