Andrey
Andrey

Reputation: 6377

how to use `if` worksheet function in vba?

My code is as follows:

Function my_if() As Boolean
    my_if = Application.WorksheetFunction.If(True, True, False)
End Function

But I am getting #VALUE as a result. Why is that?

Upvotes: 0

Views: 502

Answers (1)

John Coleman
John Coleman

Reputation: 52008

If isn't one of the functions in the WorksheetFunction object. Rather than writing your own my_if function, just use the built-in VBA function IIF instead. Or, you could do:

Function my_if() As Boolean
    my_if = IIf(True, True, False)
End Function

Upvotes: 1

Related Questions