VBA Tip: The IIF Statement

The IIf statement is a shorthand type of If..Then..Else statement. It evaluates a conditional expression then returns one value if the expression is true, and a different value if the expression is false. The syntax of the IIF statement is as follows:

IIF(<conditional expression>,<TRUE return value>,<FALSE return value>)

Here are examples for finding the maximum and minimum of two numbers. Since VBA has no built-in min or max functions these examples may find their way into many of your VBA applications.

dMax = IIf(dValue1 > dValue2, dValue1, dValue2)
dMin = IIf(dValue1 < dValue2, dValue1, dValue2)


The above IIF statements give the same result as the following If…Then…Else statements.

If (dValue1 > dValue2) Then
dMax = dValue1
dMax = dValue2
End If

If (dValue1 < dValue2) Then
dMin = dValue1
dMin = dValue2
End If

Rod Wing

Rod Wing

Rod is the Senior Systems Analyst at EnvisionCAD and has extensive experience in GIS, cartography, plotting, scanning, raster editing/manipulation, publishing, and file/data translations. He instructs MicroStation classes ranging from fundamentals to advanced.


Leave a Reply

Your email address will not be published. Required fields are marked *