G8vo7‖

TfzcA‖IIf Function

nYxhE‖Returns one of two possible function results, depending on the logical value of the evaluated expression.

FVEx2‖Syntax:


AbQZs‖IIf (Bool As Boolean, Variant1 As Variant, Variant2 As Variant) As Variant

WADQ4‖Parameters:

WA953‖Bool: Any expression that you want to evaluate. If the expression evaluates to True, the function returns the value of Variant1, otherwise it returns the value of Variant2.

4CnY7‖Variant1, Variant2: Any expression, one of which will be returned as the function result, depending on the logical evaluation.

note

JJd8f‖IIf evaluates both Variant1 and Variant2 even if it returns only one of them. If one of the expressions results in error, the function returns the error. For example, do not use IIF to bypass a possible division by zero result.


SEjHR‖Error codes:

5 Invalid procedure call

EFSA4‖Example:


vGAma‖REM Returns the maximum of 3 values
Function Max (A As Double, B As Double, C, As Double) As Double
    Max = IIf( A >= B, A, B)
    Max = IIf( C >= Max, C, Max)
End Function
9JAJ5‖REM Bad usage of function IIf
Function Inverse(A As Double) As Double
    Inverse = IIf( A = 0, 0, 1/A )
End Function

keK5U‖If or Select Case statements

KxSDU‖Switch function