使用“OR”时,IF 条件是否有任何限制?在VB中?

发布于 2025-01-03 01:56:30 字数 334 浏览 1 评论 0原文

在我的代码中我想使用 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

[注意:a 的值每次都会在 For 循环 中发生变化] 所以我只是想问一下 IF 在有限的时间内使用 OR 是否有任何限制。 或者还有其他更好的方法来做同样的事情吗?

谢谢

In my code i want to use if condition. In which i want to use "OR" around 18 times.
Like for e.g.

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

[Note : value of a is changing in For loop every time]
so i just want to ask does there any limitation in IF for using OR in limited times.
OR is there any other better way to do the same.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她说她爱他 2025-01-10 01:56:30

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

然而,您可以考虑其他编码方式。

使用 Not First 否定条件

,如果在第一种情况下什么都不做,则考虑使用 Not 语句

If Not True Then
'do somethin
'no else
End If

考虑使用 Select Case

Second,如果您正在检查完全相同的变量,您可以考虑使用Select Case,但如果您只有一种情况,那么它似乎不适合您的情况。

尝试使用搜索

最后,如果您要检查字符串,您可能最好使用在数组中搜索(如果您在 Excel 或 .包含)或在字符串中使用Instr

使用集合或字典

[编辑] 处理此问题的另一种非常好的方法是使用 VBA 的字典结构并检查a是否存在(参见MSDN 了解一些信息)。

As far as I know, there is no limitation when using OR this way.

Yet, you may consider alternative ways of coding this.

Negating a condition using Not

First, if you do nothing in the first case, then consider using the Not statement:

If Not True Then
'do somethin
'no else
End If

Consider using Select Case

Second, if you are checking the very same variable, you could either consider using a Select Case but it doesn't seem appropriate in your case if you have only one case.

Try to use a search

Eventually, if you are checking strings, you could probably better use a search within an array (with Application.Match if you are within Excel or .Contains) or within a String using Instr.

Using a collection or a dictionary

[EDIT] Another very good way to handle this would be to use the Dictionary Structure of VBA and check if a exists (see MSDN for some information).

微暖i 2025-01-10 01:56:30

这个答案只是我对 JMay 的评论的阐述,完全归功于他。我认为原始发帖人对他的问题中的“某事”的含义有所不同,其中 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

This answer is just an elaboration on my comments to JMay, full credit to him. I think the original poster meant to the "Something" in his question differ, with a being the loop variable.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文