vbaexcelselect-case

Select Case Not Or


I'm looking to carry out a Select Case with just one case - where the case is not equal to "P", "Ev" or "Af".

This is what I have so far.

Select Case Range("my_range").Offset(0, column_offset).Value
    Case Not "P", "Ev", "Af"
        'my code
 End Select

The case could equal 50 different values and I want the same action (under 'my code) to be performed for all of them unless the result is P, Ev or Af.

I've also tried Not "P", Not "Ev", Not "Af" along with replacing , with Or but to no avail.

The response each and every time is:

Run-time error '13': Type mismatch.

I know I could replace this with an if statement along the lines of...

If Range("my_range").Offset(0, column_offset).Value <> "P" And Range("my_range").Offset(0, column_offset).Value <> "Ev" And Range("my_range").Offset(0, column_offset).Value <> "Af" Then
    'my code
End if

but I'd prefer to use the Select Case option if I can.

Any thoughts anyone?

Many thanks

EDIT

I should also say I did try using

Select Case Range("my_range").Offset(0, column_offset).Value
    Case "P", "Ev", "Af"
        Exit Select
    Case Else
        'my code
End Select

but the error message:

Compile error: Expected: Do or For or Sub or Function or Property

kept popping up.


Solution

  • You cannot use Not in this way. But you can refactor to

    Select Case Range("my_range").Offset(0, column_offset).Value
        Case "P", "Ev", "Af"
            'ignore this
        Case Else
            'my code
     End Select