如何在Excel中將十進制度轉換為分鐘分秒?
有時,工作表中可能有以十進制度顯示的數據列表,現在您需要將十進制度轉換為度,分鐘和秒格式,如以下屏幕截圖所示,如何在Excel中快速進行對話?
使用VBA將十進制度數轉換為度,分,秒
請按照以下步驟使用VBA代碼將十進制度數轉換為度,分和秒。
1。 保持 ALT 按鈕並按下 F11 在鍵盤上打開一個 Microsoft Visual Basic應用程序 窗口。
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 運行VBA。
4.屏幕上顯示一個對話框,您可以選擇要轉換的單元格。 看截圖:
5。 點擊 OK,然後將所選數據轉換為度,分鐘和秒。 看截圖:
尖端:使用上面的VBA代碼將丟失原始數據,因此最好在運行代碼之前複製數據。
使用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.保存代碼並關閉窗口,選擇一個空白單元格,例如Cell A1,輸入此公式 = ConvertDecimal(“ 10°27'36”“”) (“ 10°27'36”“”代表要轉換為十進制的度,您可以根據需要進行更改),然後單擊 Enter 按鈕。 查看屏幕截圖:
相關文章
Sort comments by
#33035
This comment was minimized by the moderator on the site
0
0
#29164
This comment was minimized by the moderator on the site
0
0
#23947
This comment was minimized by the moderator on the site
0
0
#23948
This comment was minimized by the moderator on the site
0
0
#20345
This comment was minimized by the moderator on the site
0
0
#20346
This comment was minimized by the moderator on the site
0
0
#20090
This comment was minimized by the moderator on the site
0
0
#20091
This comment was minimized by the moderator on the site
0
0
#18827
This comment was minimized by the moderator on the site
0
0
#18320
This comment was minimized by the moderator on the site
0
0
#16744
This comment was minimized by the moderator on the site
0
0
#15727
This comment was minimized by the moderator on the site
0
0
#15187
This comment was minimized by the moderator on the site
0
0
There are no comments posted here yet