Thursday, February 11, 2016

Excel VBA- Case statement

EXCEL VBA: CASE STATEMENT

By Ajeet Kumar

Case Statement provides a nice way to deal with multiple mutually exclusive conditions. Although IF statements can be used to specify the conditions for the given problem, Case statement is considered better way to code such large numbers of mutually exclusive conditions. An example for Case statement is illustrated in the following code which is self-explanatory.

Sub CaseStates()
    Dim Val As Integer
    Val = Application.InputBox("Please enter a value.", "Integer", 11)
    Select Case Val
        Case 1 To 10
            MsgBox "Value is between 1 to 10 inclusively."
        Case Is = 11, 13, 15
            MsgBox "Value is either 11 or 13 or 15."
        Case Is < 50
            MsgBox "Value is less than 50 except 11, 13 and 15."
        Case Else
            MsgBox "The value is more than 49."
    End Select


End Sub 

Hot Topics