2

I have the following function which, in an Excel cell, is written as =FACING_CHANGE(live, optimal, [override]):

    Function FACING_CHANGE(live As Integer, opt As Integer, _
      Optional override As Range) As String

        If override.Value <> "" And IsNumeric(override.Value) = True Then
            opt = override.Value
        End If
        If live = opt Then
            FACING_CHANGE = "SAME " & live
        ElseIf live > opt And opt = 0 Then
            FACING_CHANGE = "DELETED"
        ElseIf live > opt And opt > 0 Then
            FACING_CHANGE = "DECREASED"
        ElseIf live < opt And live = 0 Then
            FACING_CHANGE = "ADDED"
        ElseIf live < opt And live > 0 Then
            FACING_CHANGE = "INCREASED"
        End If
    End Function

Which returns a string result. I'm having trouble with the optional override parameter. It's of type range right now because a blank cell returns a 0 if override were an integer, which is valid. I need it as an optional param because in most but not all cases it will be used with the 3rd parameter.

When I insert a breakpoint and step through, the function exits automatically at the first if statement (If override.Value...). Below are some results. Any help would be appreciated!

enter image description here

2
  • I'll take a look and let you know if I'm having trouble! Thanks for the link! Commented Dec 4, 2014 at 16:16
  • Would adding a default value for the optional parameter help your case? Commented Dec 4, 2014 at 16:22

3 Answers 3

6

When your Range optional parameter isn't specified, your function receives it as Nothing.

Then when this line runs:

If override.Value <> ""

I bet VBA is blowing up with an "object reference not set" error, because you're accessing a property on an object reference that's Nothing.

Solution is to simply check whether override Is Nothing before accessing it.

Sign up to request clarification or add additional context in comments.

Comments

2

You just need to verify if the override range object is defined or not. Try this:

Function FACING_CHANGE(live As Integer, opt As Integer, Optional override As Range) As String

    If Not override Is Nothing Then
        If override.Value <> "" And IsNumeric(override.Value) = True Then
            opt = override.Value
        End If
    End If
    If live = opt Then
        FACING_CHANGE = "SAME " & live
    ElseIf live > opt And opt = 0 Then
        FACING_CHANGE = "DELETED"
    ElseIf live > opt And opt > 0 Then
        FACING_CHANGE = "DECREASED"
    ElseIf live < opt And live = 0 Then
        FACING_CHANGE = "ADDED"
    ElseIf live < opt And live > 0 Then
        FACING_CHANGE = "INCREASED"
    End If
End Function

Comments

1

The link posted in the comments under the original question by @vba4all.com was really helpful. Here's the revised first if statement:

    If IsMissing(override) = False And VarType(override) = vbInteger Then
        If IsNumeric(override) = True Then opt = override
    End If

Also change Optional params to Variant if using IsMissing()

2 Comments

Note that this will still blow up if the function is passed Nothing - in which case the parameter will not be missing, but will not hold a valid Range reference either.
Note: IsMissing only works for Variant types, not simple types or Object (Range in this case). See: msdn.microsoft.com/en-us/library/office/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.