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.
- First, click on the character from where you want to break the line.
- Next, type a space( ).
- After that, type an underscore(_).
- 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:
- How to Add a Comment in a VBA Code (Comments in Macro)
- How to Add a New Line (Carriage Return) in a String in VBA
- Excel Visual Basic Editor (Windows + MAC) – The Ultimate Guide