XL: How to Continue a VB Statement from one Line to the Next

Last reviewed: February 3, 1998
Article ID: Q141513
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition

SUMMARY

To make Microsoft Visual Basic for Applications procedures easier to read, you may want to continue a line of code from one line to the next. To write code this way, use the line-continuation character.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

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 Microsoft Excel 5.0 and 7.0, you are limited to ten lines for continuation (that is, nine line-continuation characters). In Microsoft Excel 97 and Microsoft Excel 98, this limit is increased to 21 line- continuation characters. You can break a line at an operator, list separator, or period, as shown in the following statement:

   Workbooks("MyBook.xls").Worksheets("Sheet1") _
      .Range("A1:B5").Value = _
      var1 + 3.14159

You cannot break statements in the middle of a keyword value or a text string. If you are using a long text string and need to continue the line, you can break the string into two strings and use a combination of the concatenation operator (&) and the line-continuation character, as shown in the following example:

   var1 = "This is a long string that we are going to break"
   MsgBox var1

   var1 = "This is a long string th" & _
      "at we are going to break"
   MsgBox var1


Additional query words: 5.00 7.00 XL97 XL98 XL7 XL5
Keywords : kbcode kbprg PgmHowto
Version : WINDOWS:5.0,7.0,97; MACINTOSH:5.0,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.