See Also Example
Allows conditional execution, based on the evaluation of an expression.
Syntax 1
If condition Then thenpart [Else elsepart]
Syntax 2
If condition1 Then
[statementblock-1]
[ElseIf condition2 Then
[statementblock-2] ]
[Else
[statementblock-n] ]
End If
Remarks
Syntax 1
The single-line form is often useful for short, simple conditional tests. Syntax 1 has these parts:
Part Description
If Begins the simple If...Then control structure.
condition One of two types of expressions:
A numeric or string expression that evaluates true (nonzero) or false (0 and Null ).
A single expression of the form TypeOf object Is objecttype. The object is a form or control variable and objecttype is any one of the recognized Visual Basic object types .
Then Identifies actions to be taken if condition is satisfied.
thenpart Statements or branches performed when condition is true or if object is of the type specified by objecttype.
Else Identifies actions taken if condition is not satisfied. If the Else clause is not present, control passes to the next statement in the program.
elsepart Statements or branches performed when condition is false or object is not of the specified objecttype.
The thenpart and the elsepart fields both have this syntax:
{ statements | [GoTo] linenumber | GoTo linelabel }
The thenpart and elsepart syntax has these parts:
Part Description
statements One or more Visual Basic statements, separated by colons.
GoTo Keyword that is optional with a line number but required with a line label.
linenumber A valid program line number .
linelabel A valid program line label .
Note You can have multiple statements with a condition, but they must be on the same line and separated by colons, as in the following statement:
If A > 10 Then A = A + 1 : B = B + A : C = C + B
Syntax 2
The block form of If...Then...Else provides more structure and flexibility than the single-line form and is usually easier to read, maintain, and debug. Syntax 2 has these parts:
Part Description
If Keyword that begins the block If...Then decision control structure.
condition1 Same as condition used in the single-line form shown above.
Then Keyword used to identify the actions to be taken if a condition is satisfied.
statementblock-1 One or more Visual Basic statements executed if condition1 is true or if object is of the type specified by objecttype.
ElseIf Keyword indicating that alternative conditions must be evaluated if condition1 is not satisfied.
condition2 Same as condition used in the single-line form shown above.
statementblock-2 One or more Visual Basic statements executed if condition2 is true or if object is of the type specified by objecttype.
Else Keyword used to identify the actions taken if none of the previous conditions are satisfied.
statementblock-n One or more Visual Basic statements executed if condition1 and condition2 are both false or if object is not of the type specified by objecttype
End If Keyword that ends the block form of the If...Then.
In executing a block If, Visual Basic tests condition1, the first numeric expression or TypeOf expression. If the expression is true or if object is of the type specified by objecttype, the statements following Then are executed.
If the first expression is false or if object is not of the type specified by objecttype, Visual Basic begins evaluating each ElseIf condition in turn. When Visual Basic finds a true condition or an object that is of the type specified by objecttype, the statements immediately following the associated Then are executed. If none of the ElseIf conditions is true or no object is of the type specified in its associated objecttype, the statements following the Else are executed. After executing the statements following Then or Else, the program continues with the statement following End If.
The Else and ElseIf clauses are both optional. You can have as many ElseIf clauses as you like in a block If, but none can appear after an Else clause. Any of the statement blocks can contain nested block If statements.
Visual Basic looks at what appears after the Then keyword to determine whether or not an If statement is a block If. If anything other than a comment appears after Then, the statement is treated as a single-line If statement.
A block If statement must be the first statement on a line. The Else, ElseIf, and End If parts of the statement can have only a line number or line label in front of them. The block If must end with an End If statement.
Standard numeric and string expressions cannot be evaluated in the same conditional expression as expressions that use the form If TypeOf object Is objecttype. Use a separate ElseIf block for objects.
For example:
If a > 1 And a <= 100 Then
...
ElseIf TypeOf controlVar Is ListBox Then
...
End If
Tip Select Case may be more useful when evaluating a single expression that has several possible actions.
Select Case Statement
If...Then...Else Statement Example
The example illustrates the various forms of the If...Then...Else syntax. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim X, Y ' Declare variables.
X = InputBox("Enter a number greater than 0 and less than 1000.")
If X < 10 Then
Y = 1 ' 1 digit.
ElseIf X < 100 Then
Y = 2 ' 2 digits.
Else
Y = 3 ' 3 digits.
End If
If Y > 1 Then Unit = " digits." Else Unit = " digit."
MsgBox "The number you entered has " & Y & Unit
End Sub