Microsoft Office Tutorials and References
In Depth Information
Typing and Editing in the Code Window
Although you can use a continuation character to break a statement into two
or more lines, you can’t break a literal string in the same manner. A literal
string is text enclosed in quotation marks, as in the following example:
SomeChunk = “A literal string is text in quotation marks”
If you try to break a literal string into two lines by using a continuation
character, as the following example does, you get an error message:
SomeChunk = “A literal string is & _
text in quotation marks”
To break a literal string, you need to terminate the top line with a quotation
mark (”), followed by an ampersand (&) and then the continuation character
(_). On the next line, enclose the entire second half of the literal text in
quotation marks, as the following example shows:
SomeChunk = “A literal string is” & _
“ text in quotation marks”
Because breaking lines is entirely optional, you may never have to concern
yourself with the nitpicky details of breaking lines within literal text. When
you cut and paste code written by someone else, however, you may find that
the programmer broke up lengthy lines to make them more readable. Just be
aware that an underscore character (_) at the end of a line means “The line
below is a continuation of this line, not a new and separate statement.”
When you break literal strings, be sure to include a space between the last
word of the line you’re breaking and the first word of the next line. Putting
the space at the start of each line makes it easy to double-check for the
necessary space characters between words.
Dealing with compile errors
Each statement in VBA code must be syntactically correct and complete,
and it must be on its own line or broken correctly across several lines with
continuation characters. If you press Enter to end the line before you type
a complete, syntactically correct statement, an error message appears
onscreen.
Figure 2-14 shows what you see if you type MsgBox(“Hello World” and
press Enter. The MsgBox() statement requires a closing parenthesis, which
is missing in this example, so the editor displays an error message. In the
Code window, the faulty line is shown in red.
Search JabSto ::




Custom Search