Conversion: Degrees to Deg-Min-Sec

BillSaffian

New Member
Joined
Dec 29, 2010
Messages
25
I found the following VBA code that converts decimal degrees to degrees, minuets and seconds. I modified the code to allow the decimal value to be either positive or negative in sign. The following are several examples of calculated decimal that I'm trying to convert: (1) 39.99999974 deg = 39 deg 59' 60.00'' (2) -30.0166666666667 deg = -30 deg 0' 60.00'' (3) -29.0000000000 = "#VALUE!". I would like the first example to result in 40 deg 0' 0.00'' and the second to result in -30 deg 1' 0.00''. For these two, I know the values of 60.00 shown are rounding to a forced display value of 2 decimal places and are not exactly 60.00. For the third example, I thought the code that checked to see if the value Decimal_Deg = Degrees and if yes, forcing Minutes and Seconds to equal 0 (red font below) would work but it is not. Ideas?

Function Convert_Degree(Decimal_Deg) As Variant
With Application
'Set sign of Argument Passed
If Decimal_Deg < 0 Then
Sign = -1
Else
Sign = 1
End If
'Set degree to Integer of Argument Passed
Decimal_Deg = Abs(Decimal_Deg)
Degrees = Int(Decimal_Deg)
'Set minutes to 60 times the number to the right
'of the decimal for the variable Decimal_Deg
If Decimal_Deg = Degrees Then
Minutes = 0
Seconds = 0

Else
Minutes = (Decimal_Deg - Degrees) * 60
'Set seconds to 60 times the number to the right of the
'decimal for the variable Minute
If Minutes = Int(Minutes) Then
Seconds = 0
Else
Seconds = Format(((Minutes - Int(Minutes)) * 60), "0.00")
End If
End If
'Returns the Result of degree conversion
'(for example, 10.46 = 10~ 27' 36")
Convert_Degree = " " & Degrees * Sign & "° " & Int(Minutes) & "' " & Seconds + Chr(34)
End With
End Function
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Firstly, convert value upto whole seconds value by multiply 3600
Decimal_Deg = Abs(Round(Decimal_Deg * 3600, 2))
Then working with this whole number
VBA Code:
Option Explicit
Function Convert_Degree(Decimal_Deg) As Variant
Dim sign As String, Deg, Mins, Secs, Rems
sign = IIf(Decimal_Deg < 0, -1, 1)
Decimal_Deg = Abs(Round(Decimal_Deg * 3600, 2))
Deg = Int(Decimal_Deg / 3600)
Rems = Decimal_Deg - Deg * 3600
Mins = Int(Rems / 60)
Secs = Format(Rems - Mins * 60, "0.00")
Convert_Degree = " " & Deg * sign & "° " & Int(Mins) & "' " & Secs + Chr(34)
End Function
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,183
Members
449,071
Latest member
cdnMech

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