vba 选择大小写不或

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22987299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:43:59  来源:igfitidea点击:

Select Case Not Or

vbaexcel-vbaselect-caseexcel

提问by tom_j_uk

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

我希望只使用一个案例来执行 Select Case - 其中案例不等于“P”、“Ev”或“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.

案例可能等于 50 个不同的值,我希望'my code对所有这些值执行相同的操作(在 下),除非结果是 P、Ev 或 Af。

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

我也试过Not "P", Not "Ev", Not "Af"用替换,Or但无济于事。

The response each and every time is:

每次的回复都是:

Run-time error '13': Type mismatch.

运行时错误“13”:类型不匹配。

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

我知道我可以用一个 if 语句来替换它......

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.

但如果可以的话,我更愿意使用 Select Case 选项。

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

编译错误:预期:Do 或 For 或 Sub 或函数或属性

kept popping up.

不断弹出。

回答by Bathsheba

You cannot use Notin this way. But you can refactor to

您不能Not以这种方式使用。但是你可以重构为

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