Line Breaks

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

In VBA and VBScript code, you break a line by using the line-continuation character, an underscore (_) preceded by a space. You use line breaks to make sure that your code does not extend beyond the right edge of the Code window (usually about 60 characters).

For example, line breaks have been used in the following code so that the entire string can be viewed in the Code window without having to scroll to the right:

Dim strMessage As String
strMessage = "Fourscore and seven years ago our fathers " _
   & "brought forth, on this continent, a new nation, " _
   & "conceived in liberty, and dedicated to the " _
   & "proposition that all men are created equal."
MsgBox strMessage

Note how an additional tab character is inserted for all lines following the initial line break. This is to create a visual cue that the indented text remains a part of the portion of the string that comes before the line break.

If the line following the continued line is indented as much as the continued line would be, add one more tab to the continued line to distinguish it from the next line. For example:

If ActiveSheet.ChartObjects(1).Chart.ChartTitle = _
      ActiveSheet.Range("a2").Value Then
   MsgBox "They are equal."
End If

Be careful when you are using line-continuation characters in strings. If you must divide the string into two or more strings, place the line-continuation character between the strings, and then concatenate them using the ampersand (&). It is important to preserve all spaces in the string when it is concatenated. For example:

Sub LongString()
   ' This will form a correct SQL string.
   strSQL = "SELECT LastName, FirstName FROM Employees WHERE " _
      & "(BirthDate > #1-1-60#);"

   ' This one will be missing the space between WHERE and (BirthDate).
   strSQL = "SELECT LastName, FirstName FROM Employees WHERE" _
      & "(BirthDate > #1-1-60#);"
End Sub

Use the ampersand for all concatenation operations; never use the plus sign (+).

In HTML code you create a line break simply by entering a carriage return. The browser will ignore these line breaks when it renders the page. For example, text in this HTML page will break only where the <BR> element appears:

<BODY>
   <CENTER>
   <H2>Office Programmer's Guide
   <BR>Chapter 3
   <BR>HTML Sample Page: Line Breaks</H2>
   <HR>
   <H3>To see an example, click Source
   on the View menu.</H3>
   <BR>
   <BR>
   </CENTER>

   Fourscore and seven
   years ago our fathers
   brought forth, on this
   continent, a new nation,
   conceived in liberty, and
   dedicated to the proposition
   that all men are created equal.

</BODY>

You can also use line breaks to format a procedure's argument list. For an example of this technique, see the RemoveString procedure in the previous section.