VBA switch statement

When the user wants to execute a group of statements based on the value of Expression, the use of Switch Casestatements. Each value is called a "situation", and turned on in accordance with the test in each case variable. If the test expression and any user-specified Casedo not match, the implementation of Case Elsethe statement.

Case ElseIs Select Caseone alternative statement, however, always use a Case Elsestatement is a good programming practice.

grammar

The following is the VBScript Switchsyntax statements.

Select Case expression
   Case expressionlist1
      statement1
      statement2
      ....
      ....
      statement1n
   Case expressionlist2
      statement1
      statement2
      ....
      ....
   Case expressionlistn
      statement1
      statement2
      ....
      ....   
   Case Else
      elsestatement1
      elsestatement2
      ....
      ....
End Select

Examples

For demonstration purposes herein calculated by a function of type integer. Refer to the following diagram -

Reference Sample Code -

Private Sub switch_demo_Click()
   Dim MyVar As Integer
   MyVar = 1

   Select Case MyVar
      Case 1
         MsgBox "The Number is the Least Composite Number"
      Case 2
         MsgBox "The Number is the only Even Prime Number"
      Case 3
         MsgBox "The Number is the Least Odd Prime Number"
      Case Else
         MsgBox "Unknown Number"
   End Select
End Sub

Performing the sample code above, the following results -

 

 

Guess you like

Origin www.cnblogs.com/sunyllove/p/11348176.html