Degree Minutes Seconds Help

MrKrinkle

New Member
Joined
Mar 23, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
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

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
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
To display decimal degrees in degree, minute, second format, multiply the decimal value by 24 and use a custom format like:
[hh]º mm' ss''
Aside from being way simpler than what your macro does, the advantage of doing it this way is that the angle data can still be processed numerically.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top