Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if a value is null, then use a default value

Tags:

vba

Before I code my own function, is there a built-in function in VBA which allows me to specify a default value if a variable / function return value is null? I know of writing out an if conditional statement, but that's a little lengthy. In other languages I could do either a ternary conditional statement, or (I think in Excel) use a function to specify a default value.

e.g.

Dim v as integer
v = DefaultValue(SomeFunction(), 5) ' put 5 in v if SomeFunction returns null
like image 919
user3791372 Avatar asked Oct 26 '25 19:10

user3791372


1 Answers

This is my own function that I wrote:

Public Function DefaultValueIfNull(unknown As Variant, optional defaultValue As Variant) As Variant
    If IsNull(unknown) Then
        DefaultValueIfNull = defaultValue
    Else
        DefaultValueIfNull = unknown
    End If
End Function

Which appears to do exactly the same thing as the inbuilt function Nz(value, valueIfNull) in Access

like image 158
user3791372 Avatar answered Oct 28 '25 16:10

user3791372