Excel で度/分/秒の角度を小数点以下の角度との間で変換する方法
角度の測定値は、通常、度、分、秒 (DMS) の単位で表されます。 1 度は 60 分、1 分は 60 秒です。 いくつかの数学的計算を簡略化するために、角度測定を度単位と小数点以下の分数で表現することができます。
この記事には、小数点以下の書式設定で格納されている度の値をテキスト形式で格納されている DMS に変換するために使用できるサンプルのカスタム関数と、DMS を小数点以下の書式設定で格納されている度の値に変換するサンプル関数が含まれています。
サンプル プログラムは例示のみを目的として提供されており、商品性、特定目的に対する適合性その他について明示であると黙示であるとを問わず、マイクロソフトは一切保証をするものではありません。 この記事では、デモンストレーションされているプログラミング言語と、プロシージャの作成とデバッグに使用されるツールについて理解していることを前提としています。 Microsoft サポートプロフェッショナルは、特定のプロシージャの機能を説明するのに役立ちますが、これらの例を変更して、特定のニーズに合わせて追加の機能を提供したり、プロシージャを構築したりすることはありません。
プログラミングに習熟されていない場合は、マイクロソフトのアドバイザリ サービスまでお問い合わせください。 詳細については、次の Microsoft の Web サイトを参照してください。
マイクロソフトのアドバイザリ サービス - https://support.microsoft.com/gp/advisoryservice
利用可能なサポート オプションおよびマイクロソフトへの連絡方法の詳細については、https://support.microsoft.com を参照してください。
次の Microsoft Visual Basic for Applications カスタム関数は、小数点以下の値として書式設定された角度を受け取り、それを度、分、秒で表示されるテキスト値に変換します。
Function Convert_Degree(Decimal_Deg) As Variant
With Application
'Set degree to Integer of Argument Passed
Degrees = Int(Decimal_Deg)
'Set minutes to 60 times the number to the right
'of the decimal for the variable Decimal_Deg
Minutes = (Decimal_Deg - Degrees) * 60
'Set seconds to 60 times the number to the right of the
'decimal for the variable Minute
Seconds = Format(((Minutes - Int(Minutes)) * 60), "0")
'Returns the Result of degree conversion
'(for example, 10.46 = 10~ 27 ' 36")
Convert_Degree = " " & Degrees & "° " & Int(Minutes) & " ' " & Seconds + Chr(34)
End With
End Function
この関数を使用するには、次の例のように変換数式を作成します。
Excel を起動し、Alt キーを押しながら F11 キーを押して、Visual Basic Editor を起動します。
[挿入] メニューの [モジュール] をクリックします。
上記の Convert_Degree カスタム関数のサンプル コードをモジュール シートに入力します。
Alt キーを押しながら F11 キーを押して Excel に戻ります。
セル A1 に「10.46」と入力します。
セル A2 に数式「=Convert_Degree(A1)」と入力します。
数式は 10°27'36" を返します
次の Microsoft Visual Basic for Applications カスタム関数は、Convert_Degree 関数が返すのとまったく同じ形式で書式設定された度、分、秒のテキスト文字列 (たとえば、10° 27' 36") を受け取り、小数点以下の値として書式設定された角度に変換します。 これは、Convert_Degree カスタム関数の逆です。
警告
このカスタム関数は、秒の値が 0 の場合でも、Degree_Deg引数が度°>< 分>の><秒の形式<でない場合に失敗します。
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.
Degree_Deg = Replace(Degree_Deg, "~", "°")
degrees = CDbl(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 = CDbl(Mid(Degree_Deg, InStr(1, Degree_Deg, "°") + 1, _
InStr(1, Degree_Deg, "'") - InStr(1, Degree_Deg, "°") - 1)) / 60
' Set seconds to the number to the right of "'" that is
' converted to a value and then divided by 3600.
seconds = CDbl(Mid(Degree_Deg, InStr(1, Degree_Deg, "'") + _
1, Len(Degree_Deg) - InStr(1, Degree_Deg, "'") - 1)) / 3600
Convert_Decimal = degrees + minutes + seconds
End Function
この関数を使用するには、次の例のように変換数式を作成します。
Excel を起動し、Alt + F11 キーを押して、Visual Basic Editor を起動します。
[挿入] メニューの [モジュール] をクリックします。
上記の Convert_Decimal カスタム関数のサンプル コードをモジュール シートに入力します。
Alt キーを押しながら F11 キーを押して Excel に戻ります。
セル A1 に次の数式を入力します。
=Convert_Decimal("10° 27' 36""")
注意
秒の引用符とテキスト文字列の引用符のバランスを取るには、この数式の引数の末尾に 3 つの引用符 (""") を入力する必要があります。 セル参照には引用符は必要ありません。
数式は 10.46 を返します