LESSON SUMMARY: Understanding VBA control structures.

VBA TutorNote: The included exercises and help files are contained in 'zip' files that can be accessed throughout the lessons where appropriate. These must be unzipped to create a 'Working Directory' for each section of these tutorials. The location of the 'Working Folder' for 'PART 1', should be "C:\Documents and Settings\?yourname?\My Documents\Unzipped\vbakit1", where '?yourname?' should be your computers 'user account' name.

VBA TutorNote: This lesson refers to exercises that can be downloaded from here!


VBA has many control structures to use.

The most used ones are the 'If ... End If' and the 'For ... Next.'

With these two you should be able to build substantial control into your programs.


The 'If ... End if' CONTROL

Executes an action if a condition is true.

If Ans1 = 1 Then
' Test condition.
Score = Score + 1
' Increment variable Score if condition is true
End If
' End after action

A common form of this control is the 'If .. Else .. End If'

If Ans1 = 1 Then
' Test condition.
Score = Score + 1
' Increment variable Score if condition is true
Else
Score = Score - 1
' Decrement variable Score if condition is not true
End If


LOOK at 'Control_Structures_Demo.doc' as an example of this control in action.


The 'For ... Next' CONTROL

Repeats a statement OR a group of statements a specified number of times.

Score = 0
' Initialize variable.
For Count = 1 to 10
' Do it 10 times
Score = Score + 1
' Increment variable Score
Next Count


LOOK at 'Control_Structures_Demo.doc' as an example of this control in action.


The 'While ... Wend' CONTROL

Executes a series of statements as long as a given condition is True.

Score = 0
' Initialize variable.
While Score < 10
' Test value of variable Score
Score = Score + 1
' Increment variable Score
Wend
' End While loop when Counter > 10



LOOK at 'Control_Structures_Demo.doc' as an example of this control in action.


The 'Do ... Loop' CONTROL

Repeats a block of statements while a condition is True or until a condition becomes True.

Score = 0
' Initialize variable
Do
Score = Score + 1
' Increment variable Score
Loop Until Score = 10
' Exit loop when Score is 10


LOOK at 'Control_Structures_Demo.doc' as an example of this control in action.


The 'Select Case ... End Select' CONTROL

Executes a statement based on the value of a variable.

Select Case Score
Case is = 0
' Score is still 0
Msgbox ("Keep trying!")
Case 1To 3
' Score is between 1 and 3.
Msgbox ("Making a good start!")
Case 5, 7
' Score is either 5 or 7.
Msgbox ("Nearly there!")
Case Is > 8 And Number < 10
' Score is 9
Msgbox ("Getting close! ")
Case Else
' Score is one of the other values.
Msgbox ("Keep going! ")
End Select


LOOK at 'Control_Structures_Demo.doc' as an example of this control in action.


EXERCISES:

1. First look at 'Control_Structures_Demo.doc' to see examples of each of these control stuctures in action.

2. Then look at 'VBA Exercise1.doc' and 'VBA Exercise2.doc' to see examples of the 'If .. End If' control in action. When you understand this code, go on to look at the more advanced code in 'VBA Exercise3.doc'.

2. Rename 'Control_Structures_Demo.doc', then alter the coding for each of the control structures in turn to see how they work.

BE CAREFUL not to create a loop that WILL NOT END. Such a loop will continue to loop, as Buzz Lightyear once said, "until infinity and beyond".

FOR EXAMPLE

Score = 12
' Initialize variable
While Score <> 10
' Test value of variable Score
Score = Score + 2
' Increment variable Score
Wend
' Will never be 10 to exit