Home » excel » Excel VBA function giving "Argument not Optional"

Excel VBA function giving "Argument not Optional"

Posted by: admin April 23, 2020 Leave a comment

Questions:

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

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

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?

How to&Answers:

This is working perfectly fine for me.

Note the revision, change ra2decimal to dec2decimal.

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

Answer:

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).