Table of Contents
To add a new line to a message box in VBA, you can use the vbCrLf constant to insert a line break between your text. This can help make your message more readable and easier to understand. An example of how to use it is: MsgBox “This is Line 1” & vbCrLf & “This is Line 2” This will display two separate lines in the message box.
You can use the vbNewLine statement in VBA to add a new line to a message box.
Here are two common ways to use this statement:
Method 1: Add One New Line to Message Box
Sub MsgBoxAddLine()
MsgBox "This is the first line " & vbNewLine & "This is the second"
End Sub
Method 2: Add Multiple New Lines to Message Box
Sub MsgBoxAddLine()
MsgBox "This is the first line " & vbNewLine & vbNewLine & "This is the second"
End Sub
The following examples show how to use the vbNewLine statement in practice.
Example 1: Create Message Box with No New Lines
We can use the following syntax to create a message box that has one long sentence with no new lines:
Sub MsgBoxAddLine()
MsgBox "This is the first line and I wish I could add a second line because this sentence is getting really long"
End Sub
When we run this macro, we receive the following output:
Notice that the sentence in the message box simply starts on a new line once it reaches a certain length.
Example 2: Create Message Box with One New Line
We can use the following syntax to create a message box that creates a new line after the string “This is the first line”:
Sub MsgBoxAddLine()
MsgBox "This is the first line " & vbNewLine & "This is the second"
End Sub
When we run this macro, we receive the following output:
By using the vbNewLine statement, we were able to start a new line in the message box.
Example 3: Create Message Box with Multiple New Lines
We can use the following syntax to create a message box that creates two new lines after the string “This is the first line”:
Sub MsgBoxAddLine()
MsgBox "This is the first line " & vbNewLine & vbNewLine & "This is the second"
End Sub
When we run this macro, we receive the following output:
By using the vbNewLine statement twice we were able to insert two new lines in the message box.
This effectively created one empty line between the two strings in the message box.