Skip to Content
VB & VBA in a Nutshell: The Language
book

VB & VBA in a Nutshell: The Language

by Paul Lomax
October 1998
Intermediate to advanced
656 pages
16h 10m
English
O'Reilly Media, Inc.
Content preview from VB & VBA in a Nutshell: The Language
If...Then...Else Statement

Named Arguments

No

Syntax

If condition Then
    [statements]
[ElseIf condition-n Then
    [elseifstatements] ...
[Else
    [elsestatements]]
End If

Or, you can use the single-line syntax:

If condition Then [statements] [Else elsestatements]

condition

Use: Required

Data Type: Boolean

An expression returning either True or False or an object type.

statements

Use: Optional

Program code to be executed if condition is True.

condition-n

Use: Optional

Same as condition.

elseifstatements

Use: Optional

Program code to be executed if the corresponding condition-n is True.

elsestatements

Use: Optional

Program code to be executed if the corresponding condition or condition-n is False.

Description

Executes a statement or block of statements based on the Boolean (True or False) value of an expression.

Rules at a Glance

  • If condition is True, the statements following the If are executed.

  • If condition is False and no Else or ElseIf statement is present, execution continues with the corresponding End If statement. If condition is False and ElseIf statements are present, the condition of the next ElseIf is tested. If condition is False, and an Else is present, the statements following the Else are executed.

  • In the block form, each If statement must have a corresponding End If statement. ElseIf statements don't have their own End If. For example:

    If condition Then
       statements
    ElseIf condition Then
       statements
    End If
  • ElseIf and Else are optional, and any number of ElseIf and Else statements ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Absolute Beginner's Guide to VBA

Absolute Beginner's Guide to VBA

Paul McFedries
Visual Basic 2015 Unleashed

Visual Basic 2015 Unleashed

Alessandro Del Sole

Publisher Resources

ISBN: 1565923588Catalog PageErrata