How to Add a New Line to a Message Box (MsgBox) in Excel VBA Macros

Add to Favorites

I'll show you how to create a message box popup window in Excel that contains text on multiple lines.  This allows you to do the same thing as hitting the "enter" key when writing a message to go to a new line.


The most basic MsgBox code in Excel vba is like this:

Sub test()

MsgBox "Hi! This is a message box"

End Sub

This outputs a simple popup message box window when run.


This macro adds a new line in the message box:

Sub test2()

MsgBox "Hi!" & vbNewLine & "This is a message box"

End Sub


Note the vbNewLine that was added.  This is what actually adds the new line to the code.  Also, note that you have to use double quotes and an ampersand (&) before you can input the vbNewLine text and then you need to do the same thing again in order to input the rest of the message.


Adding as many lines as needed is really easy.  Here is another sample with two new lines, which makes a new blank line appear between the two parts of the message.

Sub test3()

MsgBox "Hi!" & vbNewLine & vbNewLine & "This is a message box"

End Sub


Hope this helps! :)

Similar Content on TeachExcel
Macro to add a New Line to Message Box Pop-up Windows in Excel
Macro: This is a very simple Message Box, pop-up window, macro for Excel that illustrates how to ...
How to Create and Manage a Chart in Excel
Tutorial: In this tutorial I am going to introduce you to creating and managing charts in Excel. Bef...
How to Add Boxes, Buttons, Arrows, Speech-Bubbles, Hearts, and More to a Spreadsheet in Excel
Tutorial: In this tutorial I am going to cover inserting and editing Shapes in an Excel workbook, as...
Excel Macro to Save a Specific Worksheet as a New File
Macro: This Excel Macro allows you to save a specific worksheet within the Excel Workbook to its ...
How to Add Formatting to Cells and Data in Excel Styles, Fonts, Colors, & More
Tutorial: In this tutorial I will cover how to use the various formatting tools in Excel. The Format...
Add a Drop Down Menu to a Cell in Excel - Data Validation Macro
Macro: Add a drop down menu or list to a cell in Excel with this free Excel macro. This is a...