In VBA, there are three different (constants) to add a line break.
vbNewLine inserts a newline 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 character 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 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 string or values, like, you have in the following code, it inserts a new line.
Range("A1") = "Line1" & vbCrLf & "Line2"
vbLf constant stands for line feed character, and when you use it within two strings, it returns 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 carriage return character that you can use to insert a new line in a message box.
MsgBox "Line1" & vbCr & "Line2"
- How to Add Line Break in a VBA Code (Single Line into Several Lines)
- How to AutoFit (Rows, Column, or the Entire Worksheet) using VBA
- How to Clear Contents from a (Cell, Range, or Entire Worksheet) using VBA
If you are preparing for an interview, make sure to check out these VBA interview questions.