CHAPTER 3 - EXCEL VBA IF STATEMENTS



AD

3.1 Excel VBA If-Then-Else

You will use If – Then statements often. The general form of the statement is:

If condition Then true_instructions [Else false_instructions]

The following program will prompt you with an Input Box for a number between 1 and 10. If the number is greater than 5, print Watch late news. Otherwise print Watch morning news.
Note: The If statement does not fit on one line. By placing a space and underscore at the end of the line, you can continue the text to the next line.
Notice the third line. The InputBox has the form: InputBox (“Prompt text”). The variable number is set equal to the value you type into the InputBox.

Sub Test8()
Dim number As Long
number = InputBox (“Enter a number between 1 and 10.”)
   If number > 5 Then MsgBox “Watch late news.”
_       Else MsgBox “Watch morning news.”
End Sub




3.2 Excel VBA If-Then

Sometimes a very short If – Then statement is useful. It takes the form:

If condition Then true-instructions

The following program will prompt you with an InputBox for a number between 1 and 10. If the number is greater than 5 then the message box prints Watch late news. Otherwise, you go to End Sub.

Sub Test9()
Dim number As Long
number = InputBox (“Enter a number between 1 and 10.”)
If number > 5 Then MsgBox “Watch late news.”
End Sub




3.3 Excel VBA If-Then-Else

This construct makes use of nested If statements. It takes the general form:

If condition Then
statement
Else
  If condition Then
  statement
  Else
    If condition Then
    statement
    End If
  End If
End If

In the next subroutine an InputBox will ask you to enter a number between 1 and 10. If the number is less than 3 then a message box will say Watch morning news. If the number is equal to 3 then a message box will say Watch noon news. If the number is greater than 3 then a message box will say Watch late news.

Sub Test10()
Dim number As Long
number = InputBox (“Enter a number between 1 and 10.”)
If number < 3 Then
MsgBox “Watch morning news.”
Else
   If number = 3 Then
   MsgBox “Watch noon news.”
   Else
      If number > 3 Then
      MsgBox “Watch late news.”
      End If
   End If
End if
End Sub




3.4 Excel VBA If-ElseIf-Else

The If-ElseIf-Else structure exists because it is more efficient. In our preceding examples of If statements all the statements are read. In the If-ElseIf-Else structure the computer stops reading conditions when a true condition is met.

It takes the general form:

If condition Then
   true_instructions
ElseIf condition Then
   alternate_instructions
Else
   alternate_instructions
End If



The program below duplicates the results of the Nested If – Then – Else program.


Sub Test11()
Dim number As Long
number = InputBox (“Enter a number between 1 and 10.”)
If number < 3 Then
   MsgBox “Watch morning news.”
ElseIf number = 3 Then
   MsgBox “Watch noon news.”
Else
   MsgBox “Watch late news.”
End If
End Sub




3.5 Excel VBA Select Case statements

A Select Case statement does not have If in it, but it acts like a nested If statement.

The Select Case statement is a third way of executing the program above. It is useful for choosing between three or more options.

It takes the following general form:

Select Case test_expression
  Case Is test_expression condition
    instruction statement
  Case Is test_expression condition
    instruction statement
  Case Is test_expression condition
    instruction statement
  Case Is test_expression condition
    instruction statement
End Select


The following program repeats the results of the two programs above.

Sub Test12()
Dim number As Long
number = InputBox (“Enter a number between 1 and 10.”)
Select Case number
   Case Is < 3
      MsgBox “Watch morning news.”
   Case Is = 3
     MsgBox “Watch noon news.”
   Case Is > 3
      MsgBox “Watch late news.”
End Select
End Sub

theSurfDragon.com


Excel Navigation

Table of Contents
Ch1-Introduction
Ch2-Loops
Ch3-If Statements
Ch4-Functions
Ch5-Subs & Functions
Ch6-Read & Write
Ch7-Operators
Ch8-Built-in Functions
Ch9-Built-in Examples
Ch10-Debugging
Ch11-Running Subs
Ch12-Sample Programs
Ch13-WS Formulas
Ch14-Questions