Share via

Custom Function in VBA - Error Message

Anonymous
2018-07-30T19:11:52+00:00

I would consider myself a pretty experienced excel user, but I don't know anything about VBA. I am trying to create a simple function below

Function IFZERO(number As Double, new_value)

    If (number = 0) Then

        new_value

    Else

        number

    End If

End Function

But I keep getting the following error message

"Compile error: Expected Sub, Function, or Property"

Does anyone know why I am getting this? Debugging doesn't help me.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

6 answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2018-07-31T19:11:37+00:00

    The problem is that #N/A is an error value. This blows up the function. To get around this, test for an error first:

    Function IFZERO(number, new_value)

        If IsError(number) Then

            IFZERO = new_value

        ElseIf number = 0 Then

            IFZERO = new_value

        Else

            IFZERO = number

        End If

    End Function

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2018-07-31T13:16:15+00:00

    Thanks! That's what I was expecting, but what tripped me up is that #N/As still give me a #VALUE! error. I actually removed the "As Double" from the formula because I want this formula to really work for all date types -- i.e., in all cases, if the input isn't the number zero, then return the input (number, string, or otherwise), else if the input is the number 0, return what you tell it to return. How to I make sure the input #N/A still returns #N/A because #N/A does not equal 0?

    Was this answer helpful?

    0 comments No comments
  3. HansV 462.6K Reputation points
    2018-07-30T21:53:40+00:00

    How are you using the function? If you want to use it in a cell formula, that formula should look like the following examples:

    =IFZERO(A1,"tadaa")

    If A1 contains a non-zero number, the formula will return that number.

    If A1 contains 0, the formula will return the text string "tadaa" (without quotes).

    If A1 contains a non-numeric value, the formula will return #VALUE!

    =IFZERO(B1,C1)

    If B1 contains a non-zero number, the formula will return that number.

    If B1 contains zero, the formula will return the value of C1.

    If B1 contains a non-numeric value, the formula will return #VALUE!

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2018-07-30T20:35:58+00:00

    Thanks! I'm no longer receiving the  "Compile error: Expected Sub, Function, or Property" message, but I am still receiving a #VALUE error. Any idea why?

    Was this answer helpful?

    0 comments No comments
  5. HansV 462.6K Reputation points
    2018-07-30T19:57:24+00:00

    You have to assign a value to the function name:

    Function IFZERO(number As Double, new_value)

        If number = 0 Then

            IFZERO = new_value

        Else

            IFZERO = number

        End If

    End Function

    Was this answer helpful?

    0 comments No comments