dcme
dcme

Reputation: 27

Select Case with another boolean check

I know how to do a select case with no issue. But:

Dim xfld As Boolean
Dim ALP As String
xfld = True
ALP = "D"
Select Case ALP
    Case "A"
        MsgBox("A")
    Case "B"
        MsgBox("B")
    Case "D" And xfld
        MsgBox("D1")
    Case "E" And Not xfld
        MsgBox("E2")
    Case Else
        MsgBox("C")
End Select

I want to do additional checks on the CASE line if possible. I know i can do an extra IF statement in it, but considering I am comparing about 10 different things I would prefer to not have to put an IF statement in each line (if xfld then print "E2" end if).

I know AND doesn't work, and the & compiles, but doesn't seem to do what I want.

Any ideas?

Upvotes: 0

Views: 281

Answers (2)

dbasnett
dbasnett

Reputation: 11773

Either of the two comments on the OP would work. Additionally you could do it this way.

    Dim xfld As Boolean
    Dim ALP As String
    xfld = True
    ALP = "D"
    Select Case True
        Case ALP = "A"
            MsgBox("A")
        Case ALP = "B"
            MsgBox("B")
        Case ALP = "D" AndAlso xfld
            MsgBox("D1")
        Case ALP = "E" AndAlso Not xfld
            MsgBox("E2")
        Case Else
            MsgBox("C")
    End Select

Upvotes: 2

Heitor Badotti
Heitor Badotti

Reputation: 188

Use comma "," instead of "And" or "&".

Dim xfld As Boolean
Dim ALP As String
xfld = True
ALP = "D"
Select Case ALP
    Case "A"
        MsgBox("A")
    Case "B"
        MsgBox("B")
    Case "D", xfld
        MsgBox("D1")
    Case "E", Not xfld
        MsgBox("E2")
    Case Else
        MsgBox("C")
End Select

Upvotes: -1

Related Questions