2013-05-15 72 views
-1

我已經GOOGLE了很多,但沒有找到任何解決方案。我想在Excel中將Lat Long數據(度,分,秒)轉換爲十進制數。如何在Excel中將度數/分鐘轉換爲小數?

傳統Lat Long網數據就像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

哎呀。我在幾個地方拼錯了'degree'。現在全部修好了;-) – Bathsheba

+0

最後它的工作!太棒了!:)你是一個真正的! – netmaster

相關問題