Select Case Statement

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
Code:
    Select Case Err.Number
        Case Is <> 0 And Not 3704
 
        'Code

This case statement executes even when the second condition is met (err.number = 3704). How do I need to write it to make sure it won't execute when the error number is 3704?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi Glory,

Try with:

Code:
 Case [COLOR=Navy]Not [/COLOR]3704, [COLOR=Navy]Not [/COLOR]0

Regards
 
Upvote 0
This is very, very annoying. The messagebox appears. Why? Is it not possible to use And in a Select Case statement?

Code:
Public Sub test()
a = 1
Select Case a
    Case Is > 0 And Not 1
        MsgBox "Hey"
 
End Select
 
End Sub



Edit: Thank you. I guess I just don't understand it well enough yet.
 
Upvote 0
Glory,

A possible work-around

Select Case Err.Number
Case 1 To 3703, 3705 To 100000
'Code

HTH

M.
 
Upvote 0
This is very, very annoying. The messagebox appears. Why? Is it not possible to use And in a Select Case statement?
Code:
Public Sub test()
a = 1
Select Case a
    Case Is > 0 And Not 1
        MsgBox "Hey"
 
End Select
 
End Sub
It is possible but your syntax is incorrect. When you write Case Is > 0 And Not 1 you're saying if a is greater than (zero And Not 1). VBA evaluates the expression (zero And Not 1) before it performs the comparison in exactly the same way as you'd expect If a > b+1 to evaluate b+1 before it compared the resulting value to a.

In your code (1) evaluates to TRUE so (Not 1) evaluates to FALSE, so (zero And Not 1) - in other words (zero And FALSE) evaluates to FALSE (or zero). VBA says "is a > FALSE?" and the answer is "yes" because a=1 and FALSE=0.

This would be one way to do it:-
Code:
 Select Case TRUE
    Case a > 0 And a <> 1

You have to be very careful what you tell VBA to do because it will do exactly what you tell it rather than what you think you meant to tell it!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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