HandsOn Using the Multi Line IfThen Statement

1. Insert a new module and enter the SimpleIfThen3 procedure as shown below.

Sub SimpleIfThen3() Dim weeks As String Dim response As String weeks = InputBox("How many weeks are in a year?", "Quiz") If weeks <> 52 Then

MsgBox "The correct answer is 52."

response = MsgBox("Would you like to try again?", _

vbYesNo + vbInformation + vbDefaultButton1, _

"Continue Quiz?")

If response = vbYes Then

Call SimpleIfThen3 End If End If End Sub

In the above example, the statements between the Then and the End If keywords don't get executed if the variable weeks is equal to 52. Notice that the multi-line If.. .Then statement must end with the keywords End If. How does Visual Basic make a decision? Simply put, it evaluates the condition it finds between the If.. .Then keywords.

0 0

Post a comment