Adding a new row in VBA
We will show you how to continue the code on the next line in VBA by example. We will also show you how to go to the next line in a message box using different methods in VBA.
Adding a new row in VBA
There are two different situations in programming when we think about how to get to the next line without breaking the code. The first is when the code in a certain line exceeds the screen size for that line.
Another way is when we have to display something in a message box and we want to wrap after a certain point. If we want to maintain multi-line coding using VBA, it provides a simple method that can be used.
We need to add space, add underscore ( _
) and then start a new line. It will treat both lines as one without any error.
Sample code:
# VBA
Sub test()
MsgBox "Lorem Ipsum is simply dummy text of the printing" _
& " and typesetting industry. Lorem Ipsum has " _
& "been the industry's standard dummy text ever since the 1500s"
End Sub
Output:
From the above example, the message box does not change, this is because the underscore ( _
) is simply used to continue the code to the next line. The code executes without any issues, which means the underscore has treated the multiple lines as a single line.
vbNewLine
Adding new rows in VBA
using the
Suppose we want to add text to a new line or add line break in a message box or in the content we are displaying or saving in an excel file. There are many different methods that can be used for this purpose.
The first method used is that vbNewLine
we can add it anywhere in the line where we want to add a break.
Sample code:
# VBA
Sub test()
MsgBox "Lorem Ipsum is simply dummy text of the printing" & vbNewLine & " and typesetting industry. Lorem Ipsum has " & vbNewLine & "been the industry's standard dummy text ever since the 1500s"
End Sub
Output:
It displays as we want, but we have used msgBox in only one row. We can also add multiple new rows by using the method multiple times as shown below.
Sample code:
# VBA
Sub test()
MsgBox "Lorem Ipsum is simply dummy text of the printing" & vbNewLine & " and typesetting industry. Lorem Ipsum has " & vbNewLine & "been the industry's standard dummy text ever since the 1500s"
End Sub
Output:
Using vbNewLine
this method multiple times will add multiple lines to the message box.
Chr(10)
Adding new rows in VBA
using the
Let's discuss another method that can be used to achieve the same purpose, called Chr(10)
. We can vbNewLine
use this method just like the method.
Sample code:
# VBA
Sub test()
MsgBox "Lorem Ipsum is simply dummy text of the printing" & Chr(10) & " and typesetting industry. Lorem Ipsum has " & Chr(10) & "been the industry's standard dummy text ever since the 1500s"
End Sub
Output:
chr(10)
The method is used in vbNewLine
the same way as the method and has the same results.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Remove duplicates in VBA
Publish Date:2025/04/16 Views:104 Category:Vba
-
We will explain how to remove duplicates in VBA by example. Remove duplicates in VBA When working with an Excel worksheet that contains a lot of data, it is likely that this data contains some duplicates. Any duplicates must be removed to a
Rounding in VBA
Publish Date:2025/04/16 Views:162 Category:Vba
-
Round() We will introduce the and functions in VBA through examples RoundUp() . Round() Using the or RoundUp() function in VBA When working with Excel worksheets containing numbers and calculations, we get results that are fractions. Someti