在vb中使用"OR"时,IF条件是否有任何限制?

Pra*_*thi 5 vb6 vbscript vba

在我的代码中我想使用if条件.我想在其中使用"OR"约18次.喜欢例如

If a="something" or a="something" or a="something" or.........(up to 18 times)... then
  'do nothing
else
  'do action
end if
Run Code Online (Sandbox Code Playgroud)

[注:值一个正在改变For循环每次],所以我只想问确实有使用或有限次,如果有任何的限制.或者还有其他更好的方法来做同样的事情.

谢谢

JMa*_*Max 14

据我所知,使用OR这种方式没有任何限制.

但是,您可以考虑使用其他方法对此进行编码.

使用Not来否定条件

首先,如果您do nothing在第一种情况下,则考虑使用以下Not语句:

If Not True Then
'do somethin
'no else
End If
Run Code Online (Sandbox Code Playgroud)

考虑使用Select Case

其次,如果你正在检查相同的变量,你可以考虑使用a,Select Case但如果你只有一个案例,它似乎不适合你的情况.

尝试使用搜索

最后,如果你正在检查的字符串,你很可能更好地使用一个数组中搜索(用Application.Match,如果你是在Excel或.Contains),或在字符串中使用Instr.

使用集合或字典

[编辑]处理这个的另一个非常好的方法是使用VBA字典结构并检查是否a存在(有关一些信息,请参阅MSDN).

  • 对于String比较,Like运算符甚至正则表达式匹配可能是一个更清晰的选项,具体取决于正在测试的内容. (2认同)

tca*_*vin 6

这个答案只是对我对JMay的评论的详细阐述,完全归功于他.我认为原始海报意味着他的问题中的"Something"不同,因为a它是循环变量.

For each a in MyList

   Select Case a
   Case "something", "something2", "something3", "something4", "something5", _
        "something6", "something7", "something8", "something9", "something10", _
        "something11", "something12", "something13", "something14", "something15", _
        "something16", "something17", "something18"

       'DO NOTHING

   Case Else

       'do-something code goes here
       ' and here
       ' and here
       ' and here
   End Select

Next a
Run Code Online (Sandbox Code Playgroud)