How to Add a New Line (Carriage Return) in a String in VBA

- Written by Puneet

excelchamps-free-courses-puneet-gogia

In VBA, there are three different (constants) to add a line break.

  1. vbNewLine
  2. vbCrLf
  3. 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.

vba-new-line

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"
carriage-return-and-line-feed

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"
vblf-that-add-a-new-line

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.

Last Updated: July 01, 2024