<

Select Case Builder

Select Case Builder

There are several ways in which Select Case statements can be used in VBA, see Using Select Case statements.

Expression

The first line Select Case expects an Expression which can be selected from the combo box. Note that if the selected variable is of type String, after the Case keyword "" (space + 2 doublequotes is added), so that you can simply insert the relevant string value, while in case of a numeric type, it is left empty, you just enter any value. Depending on the type of the selected variable different additional options are available.

Select Case from Enum Builder

Select Case from Enum Builder

Select Case from Enum Builder creates a 'Select Case' statement from enumerated constants.

The simplest variant (all checkboxes false results, example enum is MailFormat) in


Select Case varMailFormat
    Case mfHTML

    Case mfPlainText

    Case mfRTF

End Select

If we check 'Single Case' the result is


Select Case varMailFormat
    Case mfHTML, mfPlainText, mfRTF

End Select

If we check 'Return values' the result is


Function MyFunction() As String
    Select Case varMailFormat
    Case mfPlainText
        MyFunction = mfPlainText
    Case mfHTML
        MyFunction = mfHTML
    Case mfRTF
        MyFunction = mfRTF
    End Select
    End Function

'Compact' makes the code use less space:


Select Case varMailFormat
Case mfPlainText: MyFunction = "PlainText"
Case mfHTML: MyFunction = "HTML"
Case mfRTF: MyFunction = "RTF"
End Select

Finally, 'Add Case Else' determines if a 'Else' case is added. The textbox 'Case Else' contains what line of code will be added in this case.