In VBA, there are three different (constants) to add a line break.
- vbNewLine
- vbCrLf
- vbLf
vbNewLine
vbNewLine inserts a new line character that enters a new line. In the below line of code, you have two strings combined by using it.
Range("A1") = "Line1" & vbNewLine & "Line2"
When you run this macro, it returns the string in two lines.
It returns the characters 13 and 10 (Chr(13) + Chr(10)). You can use a code in the following way as well to get the same result.
Range("A1") = "Line1" & Chr(13) & Chr(10) & "Line2"
But when you use vbNewLine you don’t need to use CHAR function.
vbCrLf
vbCrLf constant stands for Carriage Return and Line feed, which means Cr moves the cursor to the starting of the line, and Lf moves the cursor down to the next line.
When you use vbCrLf within two strings or values, like, you have in the following code, it inserts a new line.
Range("A1") = "Line1" & vbCrLf & "Line2"
vbLf
vbLf constant stands for line feed character, and when you use it within two strings, it returns a line feed character that adds a new line for the second string.
Range("A1") = "Line1" & vbLf & "Line2"
Add a New Line in VBA MsgBox
If you want to add a new line while using the VBA MsgBox you can use any of the above three constants that we have discussed.
MsgBox "Line1" & vbNewLine & "Line2"
MsgBox "Line1" & vbCrLf & "Line2"
MsgBox "Line1" & vbLf & "Line2"
There’s also a constant vbCr that returns a carriage return character that you can use to insert a new line in a message box.
MsgBox "Line1" & vbCr & "Line2"
vbCr won’t work if you want to enter a cell value until you apply wrap text to it.