If...Then...Else Statement

Syntax 1

If condition Then thenpart [Else elsepart]

Syntax 2

If condition Then

   [statement(s)]

ElseIf condition Then

   [statement(s)]

Else

   [statements(s)]

End If

Syntax 2
If conditional Then statement

Allows conditional statements to be executed in the code.

Example:

Sub IfTest

   ' demo If...Then...Else

   Dim msg as String

   Dim nl as String

   Dim someInt as Integer

 

   nl = Chr(10)

   msg = "Less"

   someInt = 4

 

   If 5 > someInt Then msg = "Greater" : Beep

   MsgBox “” & msg

 

   If 3 > someInt Then

       msg = "Greater"

       Beep

   Else

       msg = "Less"

   End If

   MsgBox “” & msg

 

   If someInt = 1 Then

       msg = "Spring"

   ElseIf someInt = 2 Then

       msg = "Summer"

   ElseIf someInt = 3 Then

       msg = "Fall"1

   ElseIf someInt = 4 Then

       msg = "Winter"

   Else

       msg = "Salt"

   End If

   MsgBox “” & msg

End Sub

 


Related Topics:

Select Case Statement