Select Case

D

DS

Is this right?
Select Case
Case 1 To 4 or 6 or 8 or 10
Case 5 or 7
End Select

It seems to be working....Or should it be And
Select Case
Case 1 To 4 and 6 and 8 and 10
Case 5 and 7
End Select
Thanks
DS
 
G

George Nicholson

Select Case
Case 1 To 4, 6, 8, 10
' Do this
Case 5, 7
' Do this
Case Else
' (9) Do this
End Select

In a Case statement, comma separated list acts similar to ORs, not ANDs. Any
condition not All conditions.
 
G

George Nicholson

oops:

Select Case i

where i is the value being tested, presumably an integer between 1 and 10
inclusive.
 
M

Marshall Barton

DS said:
Is this right?
Select Case
Case 1 To 4 or 6 or 8 or 10
Case 5 or 7
End Select

It seems to be working....Or should it be And
Select Case
Case 1 To 4 and 6 and 8 and 10
Case 5 and 7
End Select


Not according to Help.

Logically, the Or is what's happening, but Help states that
you need to use commas:

Case 1 To 4, 6, 8, 10
Case 5, 7

The reason you are not getting an error is because
4 Or 6 Or 8 Or 10 is a valid VBA expression with a value of
14. If you test your code with values of 5,7,9,11... you
should get the first case, which I'm sure you fo not want.
 
D

DS

Hmm, George. Like this?
Dim i As Integer
Select Case i
Case 1 To 4, 6,8,9
Case 5,7
End Select
 
D

DS

Your right, it wasn't working. I didn't get an error message. but after
testing it always defaulted to the first case. The commas work fine.
Thanks
DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top