Home > Net >  how to use `if` worksheet function in vba?
how to use `if` worksheet function in vba?

Time:10-24

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?

CodePudding user response:

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
  • Related