如何在 VBA 中继续下一行的代码

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

How to continue the code on the next line in VBA

excelvbaexcel-vba

提问by lakesh

I would like to type the mathematical forumla in VBA code which many lines. I would like to split it into many lines. How do I do it?

我想在多行的 VBA 代码中输入数学论坛。我想把它分成多行。我该怎么做?

For example:

例如:

U_matrix(i, j, n + 1) = k * b_xyt(xi, yi, tn) / (4 * hx * hy) * U_matrix(i + 1, j + 1, n) + (k * (a_xyt(xi, yi, tn) / hx ^ 2 + d_xyt(xi, yi, tn) / (2 * hx)))

is very long. would like to split it.

很长。想分割一下。

Tried this:

试过这个:

U_matrix(i, j, n + 1) = k * b_xyt(xi, yi, tn) / (4 * hx * hy) * U_matrix(i + 1, j + 1, n) 
_+ (k * (a_xyt(xi, yi, tn) / hx ^ 2 + d_xyt(xi, yi, tn) / (2 * hx)))

But not working.. Need some guidance on this..

但不起作用..需要一些指导..

回答by Stokke

To have newline in code you use _

在您使用的代码中添加换行符 _

Example:

例子:

Dim a As Integer
a = 500 _
  + 80 _
  + 90

MsgBox a

回答by Helix Quar

(i, j, n + 1) = k * b_xyt(xi, yi, tn) / (4 * hx * hy) * U_matrix(i + 1, j + 1, n) + _
(k * (a_xyt(xi, yi, tn) / hx ^ 2 + d_xyt(xi, yi, tn) / (2 * hx)))

From ms support

To continue a statement from one line to the next, type a spacefollowed by the line-continuation character [the underscore character on your keyboard (_)].

You can break a line at an operator, list separator, or period.

来自 ms 支持

要将语句从一行继续到下一行,请键入一个空格,后跟行继续符 [键盘上的下划线字符 (_)]。

您可以在运算符、列表分隔符或句点处换行。

回答by IInspectable

In VBA (and VB.NET) the line terminator (carriage return) is used to signal the end of a statement. To break long statements into several lines, you need to

在 VBA(和 VB.NET)中,行终止符(回车)用于表示语句结束。要将长语句分成几行,您需要

Use the line-continuation character, which is an underscore (_), at the point at which you want the line to break. The underscore must be immediately preceded by a space and immediately followed by a line terminator(carriage return).

(From How to: Break and Combine Statements in Code)

在您希望换行的点使用下划线 (_) 的续行字符。下划线的前面必须紧跟一个空格,然后紧跟一个行终止符(回车)。

(来自How to: Break and Combine Statements in Code

In other words: Whenever the interpreter encounters the sequence <space>_<line terminator>, it is ignored and parsing continues on the next line. Note, that even when ignored, the line continuation still acts as a token separator, so it cannot be used in the middle of a variable name, for example. You also cannot continue a comment by using a line-continuation character.

换句话说:每当解释器遇到序列时,它就会被忽略并在下一行继续解析。请注意,即使被忽略,行继续符仍然充当标记分隔符,因此它不能用于例如变量名的中间。您也不能使用换行符来继续注释。<space>_<line terminator>

To break the statement in your question into several lines you could do the following:

要将问题中的陈述分成几行,您可以执行以下操作:

U_matrix(i, j, n + 1) = _
     k * b_xyt(xi, yi, tn) / (4 * hx * hy) * U_matrix(i + 1, j + 1, n) + _
     (k * (a_xyt(xi, yi, tn) / hx ^ 2 + d_xyt(xi, yi, tn) / (2 * hx)))

(Leading whitespaces are ignored.)

(忽略前导空格。)

回答by Curt Grasso

If you want to insert this formula =SUMIFS(B2:B10,A2:A10,F2)into cell G2, here is how I did it.

如果您想将此公式=SUMIFS(B2:B10,A2:A10,F2)插入到单元格 G2 中,我就是这样做的。

Range("G2")="=sumifs(B2:B10,A2:A10," & _

"F2)"

To split a line of code, add an ampersand, space and underscore.

要拆分一行代码,请添加与号、空格和下划线。