多行 If 语句

发布于 2024-12-18 11:47:44 字数 87 浏览 1 评论 0原文

我有一个包含多个条件的 if 语句。我无法在一个窗口视图中看到它们全部。有没有办法将它们分开在不同的行上,或者它们必须全部写在一行中?

I have an if statement with multiple conditions. I cannot see them all in a single window view. Is there a way to separate them on different lines or do they have to be written all in one line?

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

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

发布评论

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

评论(5

小伙你站住 2024-12-25 11:47:44

VBA 行继续符是下划线 _

if ( _
    (something) _
    or (somethingelse) _
) 

The VBA line-continuation character is an underscore _

if ( _
    (something) _
    or (somethingelse) _
) 
节枝 2024-12-25 11:47:44

可以使用续行符 _

这些都是一样的:

If Something Or  SomethingElse Or AnotherThing Then

If Something Or SomethingElse _
   Or AnotherThing Then

If Something Or _
   SomethingElse Or _
   AnotherThing Then

You can use the line continuation character _

These are all the same:

If Something Or  SomethingElse Or AnotherThing Then

If Something Or SomethingElse _
   Or AnotherThing Then

If Something Or _
   SomethingElse Or _
   AnotherThing Then
对你的占有欲 2024-12-25 11:47:44

与上面一样,您可以使用下划线分解一个“IF”语句中的长条件集。

如果条件太多,我觉得阅读起来有困难,我会用 BOOLEANS 来表示条件。看起来代码有点多,但我觉得更容易阅读。

如果我有一个需要执行以下操作的场景:

IF (X = something1) OR (X = something2) OR (X = something2) OR (X = something2) OR (X = something2) OR (X = something2) OR (X = something2) OR (X = something2) THEN
END IF

我可以这样做:

IF _
    (X = something1) _
    OR (X = something2) _
    OR (X = something3) _
    OR (X = something4) _
    OR (X = something5) _
    OR (X = something6) _
    OR (X = something7) _
    OR (X = something8) _
THEN
END IF

...或者我可以这样做...

blnCondition1 = (X = something1)
blnCondition2 = (X = something2)
blnCondition3 = (X = something3)
blnCondition4 = (X = something4)
blnCondition5 = (X = something5)
blnCondition6 = (X = something6)
blnCondition7 = (X = something7)
blnCondition8 = (X = something8)

IF blnCondition1 OR blnCondition2 OR blnCondition3 OR blnCondition4 OR blnCondition5 OR blnCondition6 OR blnCondition7 OR blnCondition8 THEN
END IF

当然,定义了示例 BOOLEAN 变量,我将使用而不是像 blnCondition1 这样的名称有意义的名字。

再说一次,这只是我的一个偏好。

Like the above, you can break up the long set of conditions in one "IF" statement by using an underscore.

If there are too many conditions and I find it challenging to read, I use BOOLEANS to represent the conditions. It seems like there is more code, but I find it easier to read.

If I have a scenario where I need to do the following:

IF (X = something1) OR (X = something2) OR (X = something2) OR (X = something2) OR (X = something2) OR (X = something2) OR (X = something2) OR (X = something2) THEN
END IF

I could do this:

IF _
    (X = something1) _
    OR (X = something2) _
    OR (X = something3) _
    OR (X = something4) _
    OR (X = something5) _
    OR (X = something6) _
    OR (X = something7) _
    OR (X = something8) _
THEN
END IF

... or I could do this ...

blnCondition1 = (X = something1)
blnCondition2 = (X = something2)
blnCondition3 = (X = something3)
blnCondition4 = (X = something4)
blnCondition5 = (X = something5)
blnCondition6 = (X = something6)
blnCondition7 = (X = something7)
blnCondition8 = (X = something8)

IF blnCondition1 OR blnCondition2 OR blnCondition3 OR blnCondition4 OR blnCondition5 OR blnCondition6 OR blnCondition7 OR blnCondition8 THEN
END IF

Of course, the example BOOLEAN variables are defined, and instead of names like blnCondition1, I'll use meaningful names.

Again, it's just a preference I have.

三月梨花 2024-12-25 11:47:44

用低于分数 _ 来打破它们?

Microsoft 库

上面链接中的

If ActiveSheet.ChartObjects(1).Chart.ChartTitle = _
      ActiveSheet.Range("a2").Value Then
   MsgBox "They are equal."
End If

break them with an under score _ ?

Microsoft Library

from above link

If ActiveSheet.ChartObjects(1).Chart.ChartTitle = _
      ActiveSheet.Range("a2").Value Then
   MsgBox "They are equal."
End If
倦话 2024-12-25 11:47:44

我非常确定您可以使用下划线 _ 来分隔行。

im pretty sure you can use an underscore _ to break up lines.

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