vba If 多行语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8303821/
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-08 12:14:05  来源:igfitidea点击:

If Statement With Multiple Lines

vbaif-statementline-breaks

提问by Yokhen

I have an ifstatement 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?

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

回答by Cylindric

The VBA line-continuation character is an underscore _

VBA 换行符是下划线 _

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

回答by Ken White

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

回答by Jason TEPOORTEN

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

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

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.

如果条件太多并且我觉得阅读起来很困难,我会使用 BOOLEANS 来表示条件。似乎有更多的代码,但我发现它更容易阅读。

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.

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

Again, it's just a preference I have.

同样,这只是我的一个偏好。

回答by DevilWAH

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

回答by smitec

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

我很确定你可以使用下划线_来分隔线。