Skip to main content

如何在 Excel 中將十進制度轉換為度分秒?

Author: Sun Last Modified: 2025-05-12

有時候,您可能會在工作表中有一系列以十進制度顯示的數據,現在您需要將這些十進制度轉換為度、分和秒的格式(如下方截圖所示),該如何快速在 Excel 中進行轉換呢?

decimal degrees data arrow degrees minutes seconds data

使用 VBA 將十進制度轉換為度、分、秒

使用 VBA 將度、分、秒轉換為十進制度


arrow blue right bubble 使用 VBA 將十進制度轉換為度、分、秒

請按照以下步驟使用 VBA 程式碼將十進制度轉換為度、分和秒。

1. 按住 ALT 鍵並按下鍵盤上的 F11 以打開 Microsoft Visual Basic for Application 視窗。

2. 點擊 插入 > 模組,並將 VBA 程式碼複製到模組中。

VBA:將十進制度轉換為度、分和秒

Sub ConvertDegree()
'Update 20130815
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
    num1 = Rng.Value
    num2 = (num1 - Int(num1)) * 60
    num3 = Format((num2 - Int(num2)) * 60, "00")
    Rng.Value = Int(num1) & "°" & Int(num2) & "'" & Int(num3) & "''"
Next
End Sub

3. 點擊 執行 按鈕或按 F5 鍵來運行程式碼。

4. 屏幕上會顯示一個對話框,您可以選擇要轉換的單元格。參見截圖:

vba code to select the data to convert

5. 點擊 確定,然後所選數據將被轉換為度、分和秒。參見截圖:

original data arrow converting result

提示:使用上述 VBA 程式碼會導致原始數據丟失,因此最好在運行程式碼之前先複製數據。



arrow blue right bubble 使用 VBA 將度、分、秒轉換為十進制度

有時候,您可能希望將以度/分/秒格式化的數據轉換為十進制度,以下 VBA 程式碼可以幫助您快速完成此操作。

1. 按住 ALT 鍵並按下鍵盤上的 F11 以打開 Microsoft Visual Basic for Application 視窗。

2. 點擊 插入 > 模組,並將 VBA 程式碼複製到模組中。

VBA:將度、分和秒轉換為十進制度

Function ConvertDecimal(pInput As String) As Double
'Updateby20140227
Dim xDeg As Double
Dim xMin As Double
Dim xSec As Double
xDeg = Val(Left(pInput, InStr(1, pInput, "°") - 1))
xMin = Val(Mid(pInput, InStr(1, pInput, "°") + 2, _
             InStr(1, pInput, "'") - InStr(1, pInput, _
             "°") - 2)) / 60
xSec = Val(Mid(pInput, InStr(1, pInput, "'") + _
            2, Len(pInput) - InStr(1, pInput, "'") - 2)) _
            / 3600
ConvertDecimal = xDeg + xMin + xSec
End Function

3. 保存程式碼並關閉視窗,選擇一個空白單元格,例如 A1 單元格,輸入公式 =ConvertDecimal("10° 27' 36""") ("10° 27' 36""" 表示您要轉換為十進制度的角度,您可以根據需要更改它),然後點擊 Enter 鍵。參見截圖:

enter the formula arrow result of converting degrees, minutes, seconds to decimal degrees

相關文章