VBA New Line / Carriage Return
When working with strings in VBA, use vbNewLine, vbCrLf or vbCR to insert a line break / new paragraph.
This article will also discuss how to use use the line continuation character in order to continue a statement in your actual VBA code on a new line.
Using vbNewLine
The following code shows you how you would use vbNewLine in order to put the second text string on a new line in the Immediate window:
Sub UsingvbNewLine()
Dim StringOne As String
Dim StringTwo As String
StringOne = "This is String One"
StringTwo = "This is String Two"
Debug.Print StringOne & vbNewLine & StringTwo
End Sub
The result is:
Using vbCrLf
The following code shows you how you would use vbCrLf in order to put the second text string on a new line in a shape:
Sub UsingvbCrLf()
Dim StringOne As String
Dim StringTwo As String
StringOne = "This is String One"
StringTwo = "This is String Two"
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 15, 15, 100, 50).Select
With Selection
.Characters.Text = StringOne & vbCrLf & StringTwo
End With
End Sub
The result is:
Using vbCR
The following code shows you how you would use vbCR in order to put the second text string on a new line in a message box:
Sub UsingvbCR()
Dim StringOne As String
Dim StringTwo As String
StringOne = "This is String One"
StringTwo = "This is String Two"
MsgBox StringOne & vbCr & StringTwo
End Sub
The result is:
Continuing a Statement in VBA
You can use the line continuation character (“_” aka the underscore) to continue a statement from one line to the next in your VBA code. The following code shows you how to use the line continuation character:
Sub LineContinuation ()
If Range("b1").Value > 0 Then _
Range("c1").Value = "Greater Than Zero"
End Sub