0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Access Excel 緯度 経度 130度45分15.001秒 S20度22分30秒を130.000 -20.0000に変換する関数

Posted at

グローバル対応

正規表現を参照設定します。
各数字は正規表現で抜き出し、文字をカットする手法です。
また、西経、南緯はマイナス表記になります。
このため、文字列の最初か最後にW、Sが付けばマイナスにしています。
小数点以下が続くので倍精度Double形式です

'https://oshiete.goo.ne.jp/qa/8901355.html
''正負、NESWがある場合。正規表現を使用するので参照設定
Public Function DMStoSignGbl(str) As Double
Dim DegreeLng As Long, MinDbl As Double, SecDbl As Double
Dim Reg As RegExp: Set Reg = New RegExp
Dim M As Match, MC As MatchCollection, iM As Long, lSgn As Long
Reg.Global = True: Reg.MultiLine = True: Reg.IgnoreCase = False
Reg.Pattern = "(\-[0-9]{1,3}度|[0-9]{1,3}度)" '正負を想定して
Set MC = Reg.Execute(str)
If MC.Count > 0 Then Set M = MC(0)
DegreeLng = Abs(Replace(M.Value, "度", "", 1, 1, vbTextCompare))
If DegreeLng <> Replace(M.Value, "度", "", 1, 1, vbTextCompare) Then lSgn = Sgn(Replace(M.Value, "度", "", 1, 1, vbTextCompare))
Reg.Pattern = "(^S|S$|^W|W$)" 'S,Wが最初か最後にあれば符号はマイナス
If Reg.Test(str) = True Then lSgn = Sgn(True)
Reg.Pattern = "(度[0-9]{1,2}分)"
Set MC = Reg.Execute(str)
If MC.Count > 0 Then Set M = MC(0)
MinDbl = (Replace(Replace(M.Value, "度", "", 1, 1, vbTextCompare), "分", "", 1, 1, vbTextCompare)) / 60
Reg.Pattern = "分[0-9]{0,}.[0-9]{0,}秒" 'この場合のピリオドは小数点ではなく任意の1文字
Set MC = Reg.Execute(str)
If MC.Count > 0 Then Set M = MC(0)
SecDbl = (Replace(Replace(M.Value, "分", "", 1, 1, vbTextCompare), "秒", "", 1, 1, vbTextCompare)) / 3600
If lSgn <> 0 Then DMStoSignGbl = (DegreeLng + MinDbl + SecDbl) * lSgn Else DMStoSignGbl = DegreeLng + MinDbl + SecDbl '符号に注意して返す
End Function
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?