Excel 经纬度度分秒转化为小数格式
一、采用替换算法
懒人办法,直接通过Excel替换功能完成计算。假设Excel中有这样一串数据: 现希望将它变成小数形式。打开Excel替换对话框,不断进行替换——
- 将°替换成+
- 将"E(或E)替换成/3600。"N同理。
- 将E替换成/60。N同理。
- 将替换成/60+。
- 将119+替换成=119+。30+同理。也可以用Excel的CONCAT函数将等号与每个单元格的内容连接起来。
二、采用VBA
运行以下宏即可。
transform format for longtitude and latitude
Function min(a, b)
If a > b Then
min = b
Else
min = a
End If
End Function
Sub long_la()
Dim Rng As Range
Set Rng = Selection
For Each Cell In Rng
No data
If Cell.Value = "" Then
Cell.Value = "NA"
End If
Has data
If Cell.Value <> "NA" Then
Dim Str As String
Dim temp As Integer
Dim cho As Integer
Dim Res As Double
Str = Cell.Value
°
temp = InStr(1, Str, "°")
If temp <= 0 Then
cho = MsgBox("行" + CStr(Cell.Row) + ",列" + _
CStr(Cell.Column) + " :没有符号°", vbCritical)
GoTo fail
End If
Res = Val(Mid(Str, 1, temp - 1))
Str = Mid(Str, temp + 1)
分
Dim temp1 As Integer
Dim temp2 As Integer
Dim temp3 As Integer
temp1 = InStr(1, Str, "")
If temp1 <= 0 Then
temp1 = 999
End If
temp2 = min(temp, InStr(1, Str, "‘"))
If temp2 <= 0 Then
temp2 = 999
End If
temp3 = min(temp, InStr(1, Str, "’"))
If temp3 <= 0 Then
temp3 = 999
End If
temp = min(min(temp1, temp2), temp3)
If temp <> 999 Then
Res = Res + Val(Mid(Str, 1, temp - 1)) / 60
End If
Str = Mid(Str, temp + 1)
秒
Dim temp4 As Integer
Dim temp5 As Integer
Dim temp6 As Integer
temp1 = InStr(1, Str, """")
If temp1 <= 0 Then
temp1 = 999
End If
temp2 = InStr(1, Str, "’‘")
If temp2 <= 0 Then
temp2 = 999
End If
temp3 = InStr(1, Str, "‘’")
If temp3 <= 0 Then
temp3 = 999
End If
temp4 = InStr(1, Str, "‘‘")
If temp4 <= 0 Then
temp4 = 999
End If
temp5 = InStr(1, Str, "’’")
If temp5 <= 0 Then
temp5 = 999
End If
temp6 = InStr(1, Str, "")
If temp6 <= 0 Then
temp6 = 999
End If
temp = min(min(min(min(min(temp1, temp2), temp3), temp4), temp5), temp6)
If temp <> 999 Then
Res = Res + Val(Mid(Str, 1, temp - 1)) / 3600
End If
Cell.Value = Res
End If
Next
fail:
End Sub
