Manuel
Manuel

Reputation: 19

Using multiple case is <>

Good morning, afternoon to everyone:

I've a statement in VBA like this one:

Select Case MAAX(Fila, 1)
    Case Is <> 5, Is <> 10, Is <> 15, Is <> 20, Is <> 25, Is <> 30, Is <> 35, Is <> 40
            Do whatever
End Select

But it's not working..What is wrong?

Upvotes: 1

Views: 61

Answers (2)

CallumDA
CallumDA

Reputation: 12113

See Ron's answer for a good explanation of the probem. I'd personally change your code to this:

Select Case MAAX(Fila, 1)
    Case 5, 10, 15, 20, 25, 30, 35, 40
    Case Else
        'do something
End Select

Upvotes: 1

Ron Rosenfeld
Ron Rosenfeld

Reputation: 60174

If by "not working" you mean that the statement following your CASE is always being executed, you may be misinterpreting how that statement works:

If testexpression matches ANY Case expressionlist expression, the statements following that Case clause are executed ...

Almost no matter what is in MAAX(Fila, 1), it will surely match at least one of your clauses.

You'll need to rewrite your CASE statement (or use something else), to test for the logic you really want.

If, what you really want, is that your testvalue is not any of those (5, 10, ...), you could do something like

if v<>5 and v<>10 and v<>15 ...

or, possibly

 select case v mod 5
     case is <>0  'then it is not one of those in your list, 
                  'but it could be greater than 40, so you might have to test for that

Upvotes: 2

Related Questions