Select Case confusion

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
From VBA Help:

Code:
Select Case Number    ' Evaluate Number.
Case 1 To 5    ' Number between 1 and 5, inclusive.
    Debug.Print
End Select


From my module:
Code:
    Select Case Iterations
        Case 1 Or 6
            SetProps = 6
        Case 2 Or 7
            SetProps = 24
        Case 3 Or 8
            SetProps = 42
        Case 4 Or 9
            SetProps = 60
        Case 5 Or 10
            SetProps = 78
    End Select


"Iterations" is successfully filling with the desired value, but all the case statements are being skipped even though there are matches.

Incidentally, if I do this instead:

Code:
    Select Case True
        Case Iterations = 1 Or Iterations = 6
            SetProps = 6
        Case Iterations = 2 Or Iterations = 7
            SetProps = 24
        Case Iterations = 3 Or Iterations = 8
            SetProps = 42
        Case Iterations = 4 Or Iterations = 9
            SetProps = 60
        Case Iterations = 5 Or Iterations = 10
            SetProps = 78
    End Select

The code works fine, but I thought the whole point of 'Select Case' was to evaluate the testexpression once instead of multiple times.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Solved.

Case 1, 6 is what I was looking for.

But, why doesn't "Or" work here? Can anybody explain that?

Edit: Also, is there a way to use an operator in the expressionlist argument following a Case statement?

eg Case <3
 
Last edited:
Upvote 0
Case 1, 6 is what I was looking for.

But, why doesn't "Or" work here? Can anybody explain that?

Yes. It's because Case 1 Or 6 is equivalent to Case (1 Or 6) which is equivalent to Case (110 Or 001) in binary which is equivalent to Case 7. Try ?1 or 6 in the VBA Immediate window.
 
Upvote 0
Oops, that should have read Case (001 Or 110).
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,949
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top