EXCEL VBA运行函数将angular度转换为十进制时出错

目前我正在使用VBA excel创build一个小部件来validation坐标是否位于另一个预定义和预先指定的坐标集的半径之下。

在模块中,我想在计算之前将坐标从度数转换为十进制,因为计算公式只允许坐标的十进制forms。

但是,每次我想要运行macros时,都会出现这个错误(运行时错误“5”,无效的过程调用或参数)。 然后,debuggingbutton会把我带到编码的下面:

degrees = Val(Left(Degree_Deg, InStr(1, Degree_Deg, "°") - 1)) 

为了您的信息,完整的function如下:

 Function Convert_Decimal(Degree_Deg As String) As Double 'source: http://support.microsoft.com/kb/213449 Dim degrees As Double Dim minutes As Double Dim seconds As Double Degree_Deg = Replace(Degree_Deg, "~", "°") degrees = Val(Left(Degree_Deg, InStr(1, Degree_Deg, "°") - 1)) minutes = Val(Mid(Degree_Deg, InStr(1, Degree_Deg, "°") + 2, _ InStr(1, Degree_Deg, "'") - InStr(1, Degree_Deg, "°") - 2)) / 60 seconds = Val(Mid(Degree_Deg, InStr(1, Degree_Deg, "'") + _ 2, Len(Degree_Deg) - InStr(1, Degree_Deg, "'") - 2)) / 3600 Convert_Decimal = degrees + minutes + seconds End Function 

谢谢。

在这个问题上你的善意的帮助和关注是高度赞赏的。

问候,

尼娜。

要将string转换为Double,您不要使用Val() 。 相反,你应该使用CDbl() 。 有关更多详细信息,请参见本文

编辑:你的公式也有一些小错误。 下面的代码应该可以工作:

 Function Convert_Decimal(Degree_Deg As String) As Double 'source: http://support.microsoft.com/kb/213449 Dim degrees As Double Dim minutes As Double Dim seconds As Double Degree_Deg = Replace(Degree_Deg, "~", "°") degrees = CDbl(Left(Degree_Deg, InStr(1, Degree_Deg, "°") - 1)) minutes = CDbl(Mid(Degree_Deg, InStr(1, Degree_Deg, "°") + 1, _ InStr(1, Degree_Deg, "'") - InStr(1, Degree_Deg, "°") - 1)) / 60 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