dcsimg
June 20, 2018
Hot Topics:

The IIf function

  • November 19, 2002
  • By John Percival
  • Send Email »
  • More Articles »

Have you come across the IIf function? This is what help says:

Syntax

IIf(expr, truepart, falsepart)

The IIf function syntax has these named arguments:

Part Description
expr Required. Expression you want to evaluate.
truepart Required. Value or expression returned if expr is True.
falsepart Required. Value or expression returned if expr is False.

Then you would use it as follows:

MsgBox "Hello " & IIf(strName="John", _
"John", "some one else") & ". How are you?", vbOK

This is a very mundane example, but you get the idea: it can save a lot of code if you just want to evaluate something little. Did you know that it is part of the VBA DLL? This means that if you are only writing a small prgram that does not use anything else from the VBA library then you could cut out this DLL. This is how:

Public Function IIf2(arg As Boolean, _
ret1 As Variant, ret2 As Variant) As Variant
If arg Then
    IIf2 = ret1
Else
    IIf2 = ret2
End If
End Function





Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Sitemap

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date