🚀The world's best VBA AI has evolved. ExcelMaster is now an autonomous Agent.Read more →
Back to Blog

VBA Line Continuation in Excel — The Underscore, Its Rules & the String Trap

|

VBA Line Continuation in Excel — The Underscore, Its Rules & the String Trap

TL;DR — End a line with a space and an underscore _ and VBA treats the next physical line as part of the same statement. The parser erases the _ and joins the lines before compiling, so it changes only how your source reads — never what runs. Three rules trip everyone: the space before _ is required, nothing (not even a comment) may follow it, and you cannot break inside a "string" — you close the quote, add & _, and continue.

' One logical statement, spread over three readable lines
Workbooks.Open Filename:="C:\Reports\Q3.xlsx", _
               ReadOnly:=True, _
               UpdateLinks:=False

' A long string: close the quote, join with & _, continue
MsgBox "The report finished at " & _
       Format(Now, "hh:mm") & " with no errors."

What you'll learn

  • The mental model — a source token the parser erases before compiling
  • The three source-only tokens, and where continuation sits among them
  • The mandatory space, and why nothing may follow the underscore
  • The string trap — why you cannot break inside quotes
  • Why a comment silently kills a continuation
  • Underscore vs colon — splitting one statement vs joining many
  • The opinion: continuation is for humans, so use it like one

The mental model: a token the parser erases

VBA has no line-length limit — a statement can be 500 characters long and run perfectly. So the line continuation character exists for exactly one reason: you. A space followed by an underscore _ at the end of a line tells the parser, "this statement is not finished; the next physical line continues it." Before the compiler ever sees your code, the parser removes the _, glues the lines together, and compiles one logical statement.

That is the entire mental model, and it explains every rule below. Because _ is a source-layout token consumed before compilation, it has to sit where the parser expects the statement to pause — at a natural break, with a space in front, and with genuinely nothing after it. It is punctuation for the reader, not an instruction to the machine.

Three tokens the macro never runs

Line continuation is one of three things you type that the running macro never executes. They shape the source and the compile, not the run:

Token What it does to your source What it costs the run
' comment Leaves notes and disables code the compiler deletes Nothing — it never runs
_ line-continuation Splits one long statement across many lines Nothing — the parser erases it before compiling
Option Explicit Forces every name to be declared Runs at compile time, so typos die before the macro starts

The comment and the continuation are both erased before compiling — pure source formatting. That shared nature is also why they clash, as the comment trap below shows.

Rule 1: a space before the underscore, nothing after it

The continuation is two characters in a fixed order — space, then underscore — and it must be the last thing on the line.

' RIGHT - space before _, and it ends the line
total = price + _
        tax

' WRONG - no space before the underscore
total = price +_
        tax                ' Compile error / unexpected token

' WRONG - something after the underscore (even a comment)
total = price + _ ' add tax
        tax                ' Expected: end of statement

Miss the space and +_ reads as one token; put anything after the _ — a stray space is fine, but a comment or code is not — and VBA reports Expected: end of statement. The fix is mechanical: _ sits at the very end, and the explanation goes on its own line above.

Rule 2: you cannot break inside a string

This is the trap that sends people here. An underscore inside a quoted string is just a literal underscore — it is text, not a continuation. So you cannot wrap a long string by dropping a _ in the middle of it:

' BROKEN - the _ is literal text inside the quotes
MsgBox "This is a very long message _
        that keeps going"       ' the string never closes -> compile error

' RIGHT - close the quote, concatenate with & _, continue on the next line
MsgBox "This is a very long message " & _
       "that keeps going"

To split a long string you close the quote, add & _ (concatenation, then continue), and open a new quote on the next line. Watch the trailing space inside the first quote — "message " & keeps the space that the line break would otherwise hide. This one pattern — " ... " & _ — covers ninety percent of real continuation use, because long strings (messages, SQL, paths) are what most need wrapping.

Rule 3: a comment kills a continuation

Because a comment and a continuation are both source-only tokens, they cannot share a line. An apostrophe ends the logical statement, so a _ that comes after a ' is inside the comment and does nothing:

' BROKEN - the comment swallows the continuation
total = price _  ' base price
      + tax       ' "+ tax" is now its own broken statement

' RIGHT - comment on its own line, above the statement
' base price plus tax
total = price _
      + tax

The symptom is the same Expected: end of statement on the following line, pointing at the wrong place. The rule is simple once you have hit it once: never put a comment on a continued line — lift it to a line of its own. (The comment guide covers this collision from the other side.)

Underscore vs colon: opposite tools

The _ splits one statement across many lines. The colon : does the exact opposite — it joins many statements onto one line:

' Colon: three statements crammed onto one physical line
x = 1 : y = 2 : z = 3

' Underscore: one statement relaxed across three lines
result = Application.WorksheetFunction.SumIfs( _
             data, keys, criteria)

They are constantly confused because both involve line boundaries, but they pull in opposite directions. Reach for _ to make a long statement readable; be sparing with : — packing statements onto one line hides them from breakpoints and from anyone reading the code, so keep it to trivial pairs at most. One tool serves the reader; the other usually serves nobody.

The opinion: continuation is for humans, so use it like one

_ earns its place on exactly the lines a human struggles to read on one screen: a Workbooks.Open with six named arguments, an If condition with four And clauses, a SumIfs with a dozen ranges, a SQL or connection string built from pieces. Break those at their natural joints — after a comma, before an operator, before each And — and the structure of the statement becomes visible.

But continuation is a readability tool, not a virtue in itself. Wrapping a short line for no reason, or continuing a statement fifteen times, trades one kind of unreadable for another (VBA even caps a statement at roughly two dozen continuations). The test is honest and simple: does the break make the statement easier for a person to read? If yes, use it; if not, leave the line alone. The machine never cared either way.

When the long line is a symptom, not the problem

Sometimes a statement is long because the task is genuinely complex — nested WorksheetFunction calls, sprawling criteria, a formula assembled in code. Line-continuation makes that readable, but it does not make it simple, and a 200-character statement broken across eight lines is still hard to get right. ExcelMaster lets you state the outcome instead of hand-assembling the statement — "sum sales where region is West and the date is in Q3" — and it writes and runs the code for you, backing up your file first. The long line stops being your problem, because you never have to type it.

Frequently asked questions

What is the line continuation character in VBA?

A space followed by an underscore _ at the end of a line. It tells VBA the statement continues on the next physical line. The parser removes the underscore and joins the lines into one statement before compiling, so it only changes how the source is laid out.

Why does my VBA line continuation not work?

Usually one of three reasons: there is no space before the underscore, something (often a comment) follows the underscore, or you tried to break inside a quoted string. The _ must be the last character on the line, with a space before it, and strings are split with " ... " & _, not by breaking mid-quote.

How do I continue a long string across lines in VBA?

Close the string with a quote, add a space, an ampersand and the continuation — & _ — then open a new quote on the next line: "first part " & _ then "second part". You cannot put the underscore inside the quotes, because there it is just a literal underscore.

Can I put a comment on a continued line in VBA?

No. An apostrophe ends the logical statement, so a _ after a comment does nothing and the next line breaks. Put the comment on its own line above the continued statement instead.

What is the difference between the underscore and the colon in VBA?

The underscore _ splits one statement across several lines for readability. The colon : does the opposite — it joins several statements onto one physical line. Use _ to make long statements readable; avoid : except for trivial pairs, since it hides statements from breakpoints and readers.

Tested in

Tested in: Excel 365 (Windows 11), VBA 7.1 — last verified 2026-09-06.

Related guides: VBA Comment · VBA Option Explicit · VBA MsgBox · VBA If Then Else · VBA Dim