Microsoft Office Development and Consultancy
 Home|Excel|

VBA

|C#|Finance|Tools|Newsletter|Feedback|Contact 
 VBA > Syntax > If - Then - Else< Previous | Next > 

 

Step 1 - What is an If statement ?

 
 

This is probably the most common instruction used in most programming langauges and it allows you to include decision making into your program.

 
 

If a particular condition is true, then execute this statement(s) otherwise execute this statement(s).

 
 

The condition is always a boolean expression that evaluates to either True or False.

 
 

This can be used to execute one or more lines conditionally.

 
 

You can exit an If statement at any point by using the following line

 
 
1
Exit If
   

 

Step 2 - If - Then

 
 
2
3
4
If bcondition = True Then
   statements
End If
   

 

It is common to not include the "= True" part as this is implicit

 
 
5
6
7
8
9
10
11
12
13
14
15
If bcondition Then
   statements
End If

If bInformUser = True Then
   Call Msgbox("Hello user")
End If

If (iValue < 20) Then
   Call Msgbox("The value " & iValue & " is less than 20")
End If
   

 

An alternative is the one line version although a lot of developers avoid this as it is difficult to maintain and can be confusing.

 
 
16
If expression Then statement
   

 
17
bcondition1 = (ivalue = 2)
   

 

Step 3 - If - Then - Else

 
 

The Else clause is optional but can be useful when you want to execute statements for both conditions

 
 
18
19
20
21
22
If bcondition = True Then
   statements
Else
   statements
End If
   
 

If you are only testing for one condition it is common to abbreviate this to one line.

 
 

This is not recommended as it makes you code a lot harder to read and also to change at a later date.

 
 
23
If bcondition = True Then statements
   

 

Step 4 - Nested If

 

 
24
25
26
27
28
29
30
31
32
If iMyValue = 32 Then
'do something
Else
   If iMyValue < 32 Then
'do something else
   Else
'do something different
   End If
End If
   
 

Nested If statements and long sequences of ElseIf are often hard to read and very hard to debug.

 
 

When you have a complex set of choices using the Select-Case statement is a much more appropriate.

 

 

Step 5 - ElseIf

 
 

This statement allows you to perform a related sequence of Ifs.

 
 

If the sirst If evaluates to False, then evaluate the first ElseIf.

 
 

The first ElseIf condition that evaluates to True will have its statements executed.

 
 

If none of the ElseIf conditions evaluate to True then the final Else is executed.

 
 

Rather than use a nested If statement you can often achieve the same logic by using an ElseIf construct.

 
 

The following block of code is identical to the nested if (above)

 
 
33
34
35
36
37
38
39
If iMyValue = 32 Then
'do something
ElseIf iMyValue < 32 Then
'do something else
Else
'do something different
End If
   
 

The following block can be used to test conditions on all the options. This is identical to a Select - Case

 
 
40
41
42
43
44
45
46
If bcondition1 = True Then
   statements
ElseIf bcondition2 = True Then
   statements
ElseIf bcondition3 = True Then
   statements
End If
   

 

Step 6 - Hanging Else

 


 

Step 7 - Evaluating Conditions

 
 

Lets suppose we wanted to run the following code for a negative value of x.

 
 

Instead of evaluating the first argument and then returning False all the conditions are executed which results in an error.

 
 
47
48
49
If (x < 0) And (Sqr(x) < 10) Then
'do something
End If
   
 

This is fixed in Visual Basic 2005.

 

 

Step 8 - Things to Remember

 
 
  • If you type EndIf as one word it will be automatically split into 2 for you.

     
     
  • Using logical assigments as opposed to IF statements will also help performance (ie y = (x = 5)).

     

     © Better Solutions Limited 01-Jun-2013< Previous | Top | Next >