Saturday, 5 May 2018

Select Case structure

The Select Case structure is useful for decisions involving three or more options (although it also works with two options, but using If-Then-Else structure is more efficient for that).

The syntax for the Select Case structure follows:

Example

  Select Case testexpression
  [Case expressionlist-n
    [statements-n]]
  [Case Else
    [elsestatements]]
  End Select

Don’t be scared off by this official syntax. Using the Select Case structure is quite easy.


Select Case example

The following example shows how to use the Select Case structure. This also shows another way to code the examples presented in the previous section:

Example

Sub SelectPartLength()
  Dim PartNumber As Integer
  Dim PartLength As Integer
  PartNumber = InputBox(“Please Enter part number:”)
  Select Case PartNumber
    Case Part001
      PartLength = 1
    Case Part002
      PartLength = 2
    Case Part003
      PartLength = 3
  End Select
  MsgBox “Part Length for this” & PartNumber & “is” & PartLength
End Sub

In this example, the PartNumber variable is being evaluated. The routine is checking for three different cases.

No comments:

Post a Comment