Excel VBA: Create New Line in MsgBox (6 Examples)

Example 1 – Use vbNewLine to Add New Line in MsgBox Using Excel VBA

We will show “Hello!” in the first line and “Welcome to ExcelDemy” in the second line.

Steps:

  • Press Alt + F11 to open the VBA window.
  • Go to Insert ➤ select Module.

Use vbNewLine to Add New Line in MsgBox Using Excel VBA

  • Enter the following code.
Sub NewLine_vbNewLine()
MsgBox "Hello!" & vbNewLine & "Welcome to ExcelDemy"
End Sub

Use vbNewLine to Add New Line in MsgBox Using Excel VBA

  • Go to the Developer tabselect Macros.

Use vbNewLine to Add New Line in MsgBox Using Excel VBA

  • Select the Macro name as specified in the codes.
  • Press Run.

vbNewLine will show the output in two lines.


Example 2 – Use vbCrLf to Create New Line in MsgBox Using VBA in Excel

Steps:

Sub NewLine_vbCrLf()
MsgBox "Hello!" & vbCrLf & vbCrLf & "Welcome to ExcelDemy"
End Sub

Use vbCrLf to Create New Line in MsgBox Using VBA in Excel

The constant vbCrLf will add a new line with a gap line.

Read More: Excel VBA: Develop and Use a Yes No Message Box


Example 3 – Insert vbLf to Add New Line in MsgBox Using Excel VBA

Steps:

Sub NewLine_vbLf()
MsgBox "Hello!" & vbLf & "Welcome to ExcelDemy"
End Sub

Insert vbLf to Add New Line in MsgBox Using Excel VBA

Insert vbLf to Add New Line in MsgBox Using Excel VBA

You will get the desired output.

Read More: How to Show Range Address with MsgBox in Excel VBA


Example 4 – Apply Chr to Create New Line in MsgBox Using VBA in Excel

Steps:

  • Insert a module in the VBA.
  • Enter the following code.
Sub NewLine_Chr()
MsgBox "Hello!" & Chr(13) & Chr(10) & "Welcome to ExcelDemy"
End Sub

Apply Chr to Create New Line in MsgBox Using VBA in Excel

  • Open the Macro dialog box.
  • Select the Macro name and press Run.

 

Read More: Excel VBA Code to Click OK on Message Box Automatically


Example 5 – Add New Line within the Macro in Excel VBA

In the previous methods, we didn’t break the line in the code. Here, we’ll break and add lines within the codes.

Steps:

  • Insert a module in the VBA.
  • Enter the following code.
Sub NewLine_within_Macro()
MsgBox "Hello!" & vbCrLf & vbCrLf & _
"Welcome to ExcelDemy"
End Sub

Add New Line within the Macro in Excel VBA

  • Open the Macro dialog box.
  • Select the Macro name and press Run.

Add New Line within the Macro in Excel VBA

It will add a new line with a gap line between the lines.

Read More: Excel VBA MsgBox Examples


Example 6 – Embed VBA to Add New Lines in MsgBox Using Button

6.1 Button for Single Line

We have made a dataset that represents three cells to give input – Last Name, Address and Phone number. When we click the button, it will check the cells and if gets an empty cell, it will show the message for that cell.

Embed VBA to Add New Lines in MsgBox Using Button

  • Go to the Developer tabselect Insert then select the rectangular box from the Form Controls section.

Embed VBA to Add New Lines in MsgBox Using Button

  • You will get a plus sign in your cursor, left-click and drag on the sheet to set the button size.

Embed VBA to Add New Lines in MsgBox Using Button

  • Right-click on the button and select Edit Text from the context menu to set the button name.

Embed VBA to Add New Lines in MsgBox Using Button

  • Enter a Name and click anywhere on the sheet.

  • Right-click on the button and select Assign Macro from the context menu to set a Macro.

  • Give a Macro name and press New.

  • Enter the following code.
Private Sub SingleLine_Button()
Dim WS As Worksheet
Set WS = Sheets("Single Line")
If WS.Range("C4").Value = "" Then
MsgBox "Please Insert Last Name!"
End If
If WS.Range("C5").Value = "" Then
MsgBox "Please Insert Address!"
End If
If WS.Range("C6").Value = "" Then
MsgBox "Please Insert Phone Number!"
End If
End Sub

Embed VBA to Add New Lines in MsgBox Using Button

Code Breakdown

  • We have created a Sub procedure SingleLine_Button.
  • We have declared a variable WS as Worksheet.
  • We have used three IF statements to check the cells, if the cell is filled with value then it will ignore it and if gets an empty cell then it will show the corresponding message through MsgBox.

  • Go back to your sheet and click the button.

As the Last Name field is empty, it is showing the notification message.

  • Press OK. It will check the second field.

The second field is empty too, so it added a new line to notify it.

We have filled the first field. When we clicked the button, it skipped that field and jumped to the second field to show the message.


6.2 Button for Multiple Lines

By assigning this button, we’ll be able to add multiple lines at a time in the message box.

Embed VBA to Add New Lines in MsgBox Using Button

  • Enter the following code in the macro-
Sub Multiple_Line_Button()
Dim WS As Worksheet
Set WS = Sheets("Multiple Line")
Dim Last_Name, Address, Phone, Error_msg As String
Last_Name = Len(WS.Range("C4"))
Address = Len(WS.Range("C5"))
Phone = Len(WS.Range("C6"))
If Last_Name = 0 Then
Error_msg = "Please Insert Last Name!"
End If
If Address = 0 Then
Error_msg = Error_msg & vbNewLine & "Please Insert Address!"
End If
If Phone = 0 Then
Error_msg = Error_msg & vbNewLine & "Please Insert Phone Number!"
End If
If Error_msg <> "" Then
MsgBox Error_msg, vbOKOnly, Title:="Important Caution!"
Exit Sub
End If
End Sub

Code Breakdown

  • We have created a Sub procedure Multiple_Line_Button.
  • We have declared some variables WS As Worksheet and Last_Name, Address, Phone, Error_msg As
  • We have used Len and Range to set the fields.
  • We have used If statements to check whether the fields are empty or not. If it is filled then it will skip it. If not, then will show messages through MsgBox.

  • Return to the sheet and click the button.

We can see that it is showing three lines for the three fields as they are all empty.

We have filled the first field. Now, it is only showing messages for the next 2 fields.

Read More: VBA MsgBox That Will Automatically Disappear in Excel


Download Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Md. Sourov Hossain Mithun
Md. Sourov Hossain Mithun

Md. Sourov Hossain Mithun, an Excel and VBA Content Developer at Softeko's ExcelDemy project, joined in October 2021. Holding a Naval Architecture & Marine Engineering degree from BUET, Mithun showcases expertise during his two-year tenure. With over 200 published articles on Excel topics, he earned a promotion to Team Leader, excelling in leading diverse teams. Mithun's passion extends to Advanced Excel, Excel VBA, Data Analysis, and Python programming, contributing significantly to the innovative and dynamic environment of ExcelDemy... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo