2013-05-15 2 views
-1

나는 그것을 많이 봤지만 어떤 해결책도 찾지 못했습니다. 나는 Lat Long 데이터 (도, 분, 초)를 10 진수로 변환하려고합니다.excel에서 십진수로도/분 단위로 나누는 방법은 무엇입니까?

전통적으로 위도 긴 데이터 25°43'21.3" 같다,하지만도 (°) 기호가없는 일하고 있어요 데이터베이스는이 점을 가지고 (.) degree.For가 25.43'21.3"

그래서 무엇 일 것입니다 예 - 대신 25.43'21.3"을 십진수로 변환하는 스크립트 - 25.722583333333333 ??

데이터가 (°) 기호와 함께 제공되면 다음 코드가 작동합니다.

Function Convert_Decimal(Degree_Deg As String) As Double 
    ' Declare the variables to be double precision floating-point. 
    Dim degrees As Double 
    Dim minutes As Double 
    Dim seconds As Double 
    ' Set degree to value before "°" of Argument Passed. 
    degrees = Val(Left(Degree_Deg, InStr(1, Degree_Deg, "°") - 1)) 
    ' Set minutes to the value between the "°" and the "'" 
    ' of the text string for the variable Degree_Deg divided by 
    ' 60. The Val function converts the text string to a number. 
    minutes = Val(Mid(Degree_Deg, InStr(1, Degree_Deg, "°") + 2, _ 
      InStr(1, Degree_Deg, "'") - InStr(1, Degree_Deg, _ 
      "°") - 2))/60 
    ' Set seconds to the number to the right of "'" that is 
    ' converted to a value and then divided by 3600. 
    seconds = Val(Mid(Degree_Deg, InStr(1, Degree_Deg, "'") + _ 
      2, Len(Degree_Deg) - InStr(1, Degree_Deg, "'") - 2)) _ 
      /3600 
    Convert_Decimal = degrees + minutes + seconds 
End Function 
+0

단지'''와 "°"' –

+0

어떻게 작업을 대체 할 대체? 데이터에 몇 개의 점 (.)이 포함되어 있습니다. – netmaster

+0

'InStr'은 첫 번째'.'의 위치를 ​​반환합니다. 찾았습니다. –

답변

2

이 그것을 할 것입니다 "."

Private Const vbQuote As String = """" 

Public Sub test() 
    Debug.Print answer("25.43'21.3""") 
End Sub 


Public Function answer(ByVal s As String) As Double 
    Dim degrees As Double 
    Dim minutes As Double 
    Dim seconds As Double 

    Dim dotPos As Integer 'position of first dot in the string 
    Dim commaPos As Integer 'position of comma in the string 
    Dim quotePos As Integer 'position of quote in the string 

    dotPos = InStr(s, ".") 
    commaPos = InStr(s, "'") 
    quotePos = InStr(s, vbQuote) 

    If dotPos = 0 Or _ 
     commaPos = 0 Or _ 
     quotePos = 0 Or _ 
     dotPos > commaPos Or _ 
     commaPos > quotePos Then 

     'some error handling here 
     Stop 
    End If 

    degrees = CDbl(Left(s, dotPos - 1)) 
    minutes = CDbl(Mid(s, dotPos + 1, commaPos - dotPos - 1)) 
    seconds = CDbl(Mid(s, commaPos + 1, quotePos - commaPos - 1)) 

    answer = degrees + minutes/60 + seconds/3600 

End Function 
+0

대단히 감사합니다! 그러나 스크립트는 0.722583333 원래 값이 25.722583333이 될 것입니다, 무엇이 잘못 되었습니까? 나는 A1에 25.43'21.3을 유지하고 B1 셀에 대답 = 대답 (A1)을 입력했는데, 무엇을 놓쳤는가? – netmaster

+0

나는 몇 군데에 '도'를 잘못 입력했다. 모두 지금 고쳐졌다 ;-) – Bathsheba

+0

마침내 좋아! :) 너는 유머 감각이있다! – netmaster

관련 문제