VBA New Line

Publication Date :

Blog Author :

Table Of Contents

arrow

New Line in VBA MsgBox

Aligning the sentence is very important to convey the right message to the users or readers. To make the sentence proper, we use "new paragraph" or newline as one of the techniques. It usually happens in Word documents. If you have this question, then this article eases your worry. Follow this article completely to learn about the new line in VBA.

In Excel, when we want to insert a new line character, we either press Ctrl + Enter to insert a new line break or use the CHR function with 10. In VBA programming, using newline breakers to frame sentences is almost inevitable. But the question is, how can we insert a new VBA line breaker?

VBA New Line

How to Insert New Line in VBA MsgBox?

We have seen people using several ways to insert newlines in VBA. So, in this article, we have decided to show you each of them in detail.

Before we show you how to insert a new line in VBA, let us show you why you need to insert lines in VBA. For example, look at the below image.

insert new line 1

We have framed the sentence in the message without inserting any new lines in the VBA codes. Now, look at the below image.

insert new line 2

Suppose you look at the above two images, which one looks neat and clean. Both convey the same message but having a look at both the images. You decide which is better for you and continue reading to learn the second image.

Example #1 - Insert New Line in VBA MsgBox Using “vbNewLine.”

To insert the new line in VBA, we can use the VBA Constant vbNewLine.”

As the name says, it will insert a new line between sentences or characters. For example, look at the below code.

Code:

Sub Type_Example1()

  MsgBox "Hi Welcome to VBA Forum!!!. We will show you how to insert new line in this article"

End Sub

In the above code, we have two sentences. The first one is "Hi, Welcome to VBA Forum!" And the second one is, "We will show you how to insert a new line in this article."

The above shows these sentences in a single line, only like the below image.

VBA New Line 1

When the sentences are too large, it often creates ambiguity in the readers' minds, or it doesn't look pleasant. As a result, readers do not want to read at all.

To avoid all these things, we can show the message in two lines instead of the default line after the first line sentence closes the double quotes and puts the ampersand (&) symbol.

Code:

Sub Type_Example1()

  MsgBox "Hi Welcome to VBA Forum!!!."&

End Sub
VBA New Line Example 1

After the ampersand (&) symbol, press the spacebar and get the VBA constant "vbNewLine."

Code:

Sub Type_Example1()

  MsgBox "Hi Welcome to VBA Forum!!!." & vbNewLine

End Sub
VBA New Line Example 1-1

After the constant "vbNewLine," press one more time space bar and add the ampersand (&) symbol.

Code:

Sub Type_Example1()

  MsgBox "Hi Welcome to VBA Forum!!!." & vbNewLine &

End Sub
VBA New Line Example 1-2

After the second ampersand (&) symbol, type one more space character, and add the next line sentence in double quotes.

Code:

Sub Type_Example1()

  MsgBox "Hi Welcome to VBA Forum!!!." & vbNewLine & "We will show you how to insert new line in this article"

End Sub
Example 1-3

We have done it. Run the code to see the two sentences in two lines.

Example 1-4

If you are unhappy with the single line breaker, insert one more line breaker by entering one more new line inserter in VBA Msgbox using “vbNewLine.”

Code:

Sub Type_Example1()

MsgBox "Hi Welcome to VBA Forum!!!." & vbNewLine & vbNewLine & "We will show you how to insert new line in this article"

End Sub
Example 1-5

Above bold and underlined words will insert two line breakers between sentences, and the result is as below.

Example 1-6

Example #2 - Insert New Line Using “Char (10)”

To a new line instead of "vbNewLine," we can also use the function CHR to insert a new line in VBA. For example, CHR (10) is the code to insert a new line in VBA. Below is an example of the same.

Code:

Sub Type_Example1()

MsgBox "Hi Wecome to VBA Forum!!!." & Chr(10) & Char(10) & "We will show you how to insert new line in this article"

End Sub

Example #3 - Insert New Line Using “vbCrLf, vbCr, vbLf”

We can also use the constants "vbCrLf, vbCr, vbLf" to insert the new line breaker. Below are examples of the same.

Code:

Sub Type_Example1()

MsgBox "Hi Welcome to VBA Forum!!!" & vbLf & vbLf & "We will show you how to insert new line in this article"

End Sub

Code:

Sub Type_Example1()

MsgBox "Hi Welcome to VBA Forum!!!" & vbCr & vbCr & "We will show you how to insert new line in this article"

End Sub

Code:

Sub Type_Example1()

MsgBox "Hi Welcome to VBA Forum!!!" & vbCrLf & vbCrLf & "We will show you how to insert new line in this article"

End Sub

You can download this VBA New Line Excel here. VBA New Line Excel Template