Professional Documents
Culture Documents
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:
Tried this:
Does it matter if the "_" is on the previous line? – RichS Apr 4 '14 at 5:41
3 Answers
Example:
Dim a As Integer
a = 500 _
+ 80 _
+ 90
MsgBox a
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 (_)].
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).
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)))
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