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

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

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

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.

One of the most significant benefits of using a line break is to make a 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 on the character from 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 the argument.

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 below two lines of code in a single line. You can use a colon (:) for that.

So it will be:

What’s Next

There’s More

VBA With Statement | VBA Wait and Sleep Commands | VBA Status Bar | VBA ScreenUpdating | VBA Random Number | VBA Immediate Window (Debug.Print) | VBA Concatenate | VBA Module | VBA Random Number