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
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