Search results
Feb 5, 2009 · The proper way to do this in VB is to use on of the VB constants for newlines. The main three are. vbCrLf = "\r\n". vbCr = "\r". vbLf = "\n". VB by default doesn't allow for any character escape codes in strings which is different than languages like C# and C++ which do.
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.
- Overview
- To break a single statement into multiple lines
- To place multiple statements on the same line
- See also
When writing your code, you might at times create lengthy statements that necessitate horizontal scrolling in the Code Editor. Although this doesn't affect the way your code runs, it makes it difficult for you or anyone else to read the code as it appears on the monitor. In such cases, you should consider breaking the single long statement into sev...
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) or (starting with version 16.0) a comment followed by a carriage return.
In the following example, the statement is broken into four lines with line-continuation characters terminating all but the last line.
Using this sequence makes your code easier to read, both online and when printed.
The line-continuation character must be the last character on a line. You can't follow it with anything else on the same line.
Some limitations exist as to where you can use the line-continuation character; for example, you can't use it in the middle of an argument name. You can break an argument list with the line-continuation character, but the individual names of the arguments must remain intact.
You can't continue a comment by using a line-continuation character. The compiler doesn't examine the characters in a comment for special meaning. For a multiple-line comment, repeat the comment symbol (') on each line.
Separate the statements with a colon (:), as in the following example:
In VBA, there are three different (constants) to add a line break. vbNewLine; vbCrLf; vbLf; vbNewLine. vbNewLine inserts a new line character that enters a new line. In the below line of code, you have two strings combined by using it. Range("A1") = "Line1" & vbNewLine & "Line2" When you run this macro, it returns the string in two lines.
You need to insert a line break or tab character in a string, typically for display purposes. Solution Use the NewLine property of the System.Environment class, or use the global vbTab and vbNewLine constants.
Jul 26, 2021 · When working with strings in VBA, use vbNewLine, vbCrLf or vbCR to insert a line break / new paragraph. This article will also discuss how to use use the line continuation character in order to continue a statement in your actual VBA code on a new line.
Sep 15, 2021 · You can use the line-continuation character, which is an underscore (_), to break a long line of code over several lines in your file. The underscore must be immediately preceded by a space and immediately followed by a line terminator (carriage return).