The Immediate If Function

The Immediate If function has the syntax:

IIf(Expression, TruePart, FalsePart)

If Expression is True, then the function returns TruePart. If Expression is False, the function returns FalsePart. For instance, the following code displays a dialog indicating whether or not the first row in the active worksheet is empty:

Dim rng As Range

Set rng = ActiveSheet.Rows(l)

MsgBox IIf(IsEmpty(ActiveSheet.Cells(1, 1)), "Cell is empty", "Cell is not empty")

It is very important to note that the Immediate If function always evaluates both TruePart and FalsePart, even though it returns only one of them. Hence, we must be careful about undesirable side effects. For example, the following code will produce a division by zero error because even though the IIf function returns 1/x only when x is not equal to 0, the expression 1/x is evaluated in all cases, including when x is equal to 0:

0 0

Post a comment