You are on page 1of 2

12/7/2017 excel - How to continue the code on the next line in VBA - Stack Overflow

How to continue the code on the next line in VBA

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?

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..

excel vba excel-vba

asked Apr 4 '14 at 5:37


lakesh
14.1k 40 123 218

Does it matter if the "_" is on the previous line? – RichS Apr 4 '14 at 5:41

3 Answers

To have newline in code you use _

Example:

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

MsgBox a

edited Nov 10 at 13:46 answered Apr 4 '14 at 5:46


Brad Solomon Stokke
5,283 3 12 46 798 7 13

(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 space followed by the line-
continuation character [the underscore character on your keyboard (_)].

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

answered Apr 4 '14 at 5:46


helix
361 1 14

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

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)

https://stackoverflow.com/questions/22854386/how-to-continue-the-code-on-the-next-line-in-vba 1/2
12/7/2017 excel - How to continue the code on the next line in VBA - Stack Overflow

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.

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.)

answered Jan 19 '16 at 16:50


IInspectable
21.4k 3 32 73

Upvoted for explanation in addition to answer. Commenting to add that there is an additional step if you're
trying to break up a string. If you want to have a string on two lines you need to close the quote; add an
ampersand(&), space, and underscore(_); and start the new line with another quote. Remember to include a
trailing space or leading space or you'll end up with a word mash where the two strings join. – PC_Goldman
Oct 5 at 14:06

Join Stack Overflow to learn, share knowledge, and build your career. Email Sign Up OR SIGN IN WITH Google Facebook

https://stackoverflow.com/questions/22854386/how-to-continue-the-code-on-the-next-line-in-vba 2/2

You might also like