Excel VBA 函数给出“参数不可选”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15671567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:18:33  来源:igfitidea点击:

Excel VBA function giving "Argument not Optional"

excelvbaexcel-vba

提问by Fergus Barker

I'm trying to write a VBA function for converting Declination & Right Ascension to their decimal values.

我正在尝试编写一个 VBA 函数,用于将赤纬和赤经转换为十进制值。

I have a perfectly working function for the RA but keep getting an

我有一个完美的 RA 工作功能,但不断得到一个

Argument not Optional

参数不是可选的

error.

错误。

The function is currently:

该功能目前是:

Public Function dec2decimal(Deg As Integer, Min2 As Integer, Sec2 As Integer) As Double

    Dim Dec As Double
    Dec = 0
    If Deg >= 0 Then
        Dec = (Deg + Min2 / 60 + Sec2 / 3600)
    Else
        Dec = (Deg - Min2 / 60 - Sec2 / 3600)
    End If

    ra2decimal = Dec
End Function

I'm obviously supplying it three arguements when I run it but its still not happy.

当我运行它时,我显然向它提供了三个论点,但它仍然不高兴。

Any ideas?

有任何想法吗?

采纳答案by David Zemens

This is working perfectly fine for me.

这对我来说非常好。

Note the revision, change ra2decimalto dec2decimal.

注意修订,更改ra2decimaldec2decimal.

Option Explicit
Sub testDec2Decimal()

MsgBox dec2decimal(1, 1, 1)

End Sub

Public Function dec2decimal(Deg As Integer, Min2 As Integer, Sec2 As Integer) As Double

    Dim Dec As Double
    Dec = 0
    If Deg >= 0 Then
        Dec = (Deg + Min2 / 60 + Sec2 / 3600)
    Else
        Dec = (Deg - Min2 / 60 - Sec2 / 3600)
    End If

    dec2decimal = Dec
End Function

回答by Lajos Arpad

Check the following:

检查以下内容:

  • are you sure, you are executing the function which you described, is it possible, that another function in another namespace or class is executed?

  • are you sure you pass the parameters correctly, their type is correct and their number is correct and they are separated by comma?

  • are you sure you didn't make a typo or called the function through a delegate (which is not a problem, but we should know to be able to help you).

  • 你确定,你正在执行你描述的函数,是否有可能执行另一个命名空间或类中的另一个函数?

  • 您确定您正确传递了参数,它们的类型正确,编号正确,并且它们之间用逗号分隔吗?

  • 你确定你没有打错字或通过委托调用了函数(这不是问题,但我们应该知道能够帮助你)。