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.
Additional Resources
The following tutorials explain how to perform other common tasks in VBA:
VBA: How to Find Last Used Row
VBA: How to Find Last Used Column
VBA: How to Insert Multiple Rows