Make your macros shorter – IIf

Screen Shot 2018-06-16 at 17.48.00

Using “IIf” can make your functions shorter like below:

Sub testIIF()

  Dim bLong As Boolean
  Dim bVeryLong as Boolean
  Dim strName As String

  strName = "Mazzy"

  bLong = IIf(Len(strName) > 3, True, False) 

  'And you can even nest an IIf inside another one like this
  bVeryLong = IIf(Len(strName) > 3, IIf(Len(strName) > 4, True, False), False)

End Sub

And here’s the equivalent code with a regular “If” for the first part:

Sub testIF()

  Dim bLong As Boolean
  Dim strName As String

  strName = "Mazzy"

  bLong = False
  If Len(strName) > 3 Then
    bLong = True
  End If

End Sub

Be creative but be careful not to nest too many IIfs because then your code would be very hard to read.

Keep coding!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.