30 Advanced Excel Tips for 2026

Excel Champs / VBA / How to Add a Line Break in a VBA Code (Single Line into Several Lines)
By Puneet Gogia (Microsoft MVP)

How to Add a Line Break in a VBA Code (Single Line into Several Lines)

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

To split a long line of VBA code across several lines, end the line with a space and an underscore ( _), then carry on with the next line:

MsgBox "Total sales for the month are $" & Format(Range("B10").Value, "#,##0"), _
    vbInformation, "Monthly Report"

Break the line between parts of the statement, such as after a comma or an &. You cannot break inside a string in quotes. To split a long piece of text, close the quotes and join the parts: "First part of the text " & _ on one line, then "second part" on the next.

The space before the underscore is required, and nothing can follow the underscore on that line, not even a comment. One statement can use up to 24 continuations.

To go the other way and put two short statements on one line, separate them with a colon: Range("A1").Value = 1: Range("A2").Value = 2.

If you want a line break inside text, so a message box or a cell shows it on two lines, the underscore will not do it. Join the parts with vbNewLine for a message box, or vbLf for a cell, as in MsgBox "Line 1" & vbNewLine & "Line 2".

While writing VBA codes, you might face a situation where you have a lengthy line of code, which makes it hard for everyone to read. In that case, the best way is to use the VBA Line Break Character, which you have in the below example.

vba-line-break-character

In the first macro, you have only one long line, but in the second macro, that same line is broken into three lines with line-continuation characters.

Now, if you look closely, the first two lines have an underscore at the end.

If you want to break a line into two or more, you need to type a space followed by an underscore.

use-an-underscore

One of the most significant benefits of using a line break is that it makes your code structured and easy to read.

Line Break in a VBA Code

To enter a VBA line break, you can use the following steps.

  1. First, click right after the character where you want to break the line.
  2. Next, type a space ( ).
  3. After that, type an underscore (_).
  4. In the end, hit Enter to break the line.

But there is one thing that you need to take care of: you can’t add a line break in the middle of an argument name, a keyword, or text in double quotes. Break the line between two parts of the statement, like after a comma or an operator.

vba-line-break-steps

Multiple Lines of Code on One Line

VBA also allows you to write multiple lines of code on the same line. Let’s say you want to combine the two lines of code below into a single line. You can use a colon (:) for that.

multiple-lines-of-code

So it will be:

use-a-colon

Use this only for very short lines. Putting many statements on one line makes the code harder to read and to debug, which is the opposite of what a line break does.

Break a Long Text String into Multiple Lines

As you can’t break a line inside the double quotes, you need to split a long text into two or more parts. Close the quotes, join the parts with an ampersand (&), and then add the space and underscore:

Sub long_text()
MsgBox "This is a very long message that I want to " & _
"split into two lines in the code."
End Sub

This only breaks the line in the code. The message itself still shows on one line. If you want the text to show on a new line in the message box, use vbNewLine (see VBA new line).

Rules for the Line-Continuation Character

  • Space before the underscore: without the space, VBA doesn’t treat the underscore as a line break, and you get a syntax error.
  • Nothing after the underscore: the underscore must be the last character on the line, so you can’t put a comment after it.
  • Up to 24 in a row: you can join a maximum of 24 consecutive line-continuations in one statement. Beyond that, VBA shows the “Too many line continuations” error.

Here’s a longer line broken after the commas, which is the most common way to use it:

Sub long_line()
Worksheets("Sheet1").Range("A1:A10").BorderAround _
Weight:=xlThick, _
Color:=RGB(255, 0, 0)
End Sub

Frequently Asked Questions

Why do I get a syntax error after adding an underscore?

Check three things: there’s a space before the underscore, nothing comes after it, and the break isn’t inside double quotes or in the middle of a name.

Is a line break in code the same as a new line in text?

No. The space and underscore only split the code across lines. To start a new line inside the text that VBA shows or writes in a cell, use vbNewLine, vbCrLf or Chr(10).

Does a line break slow down the code?

No. VBA reads the broken lines as one statement, so it runs exactly the same way.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel