To add a new line to a VBA MsgBox message, join the text with vbCrLf (or vbNewLine). For example, "First line" & vbCrLf & "Second line" displays the two phrases on separate lines in the dialog.
The basic MsgBox line break
A VBA MsgBox displays a prompt in a dialog box. The prompt can be one string or a string assembled from several pieces. To make a visible line break, insert a newline constant between the pieces using the ampersand concatenation operator. The key point is that the break belongs in the prompt text, not in the MsgBox arguments that choose buttons or a title.
Here is a complete Excel VBA example. It creates a three-line message and gives the dialog an information icon and a title:
Sub ShowMultilineMessage()
Dim customerName As String
Dim messageText As String
customerName = "Avery"
messageText = "Hello, " & customerName & "." & vbCrLf & _
"Your order is ready." & vbCrLf & _
"Please collect it today."
MsgBox messageText, vbInformation, "Order status"
End Sub
Before running a macro, save a copy of the workbook. Macros can change workbook contents, and their actions cannot be undone with Ctrl+Z; this particular example only displays a dialog, but the reminder matters when adapting code for other tasks.
In the example, the ampersands join text and line-break constants into one prompt. The underscore at the end of the first two assignment lines is VBA’s explicit line-continuation character: it lets the source code continue on the next editor line. It does not create a new line in the dialog. The two instances of vbCrLf create those visible breaks.
Which newline constant should you use?
For a typical Excel macro on Windows, vbCrLf is the familiar choice. The name represents a carriage return followed by a line feed. VBA also provides vbNewLine, a platform-appropriate newline constant. Either is suitable for ordinary multiline MsgBox prompts; choose one and use it consistently.
vbCrLf: a carriage return and line feed together; a clear, common choice for Windows VBA.vbNewLine: a newline constant intended to adapt to the platform running the code.vbLf: a line-feed character alone. It may work in some contexts, but is less conventional for a Windows MsgBox prompt.vbCr: a carriage-return character alone. It is usually not the clearest choice for a standard multiline dialog.
Excel for Mac also supports VBA and MsgBox, although dialog appearance can differ from Windows. If a macro must run on both systems, vbNewLine communicates the portable intent. If you specifically need a Windows-style carriage-return/line-feed pair, use vbCrLf. For a simple prompt, both normally produce the expected separate lines.
Build messages from variables
Real prompts often combine fixed instructions with values from cells or variables. Concatenate each value where it belongs and place a newline constant wherever the dialog should move to the next line. Keep the pieces readable rather than trying to put literal line breaks inside quoted VBA text.
For example, if a macro has already assigned a product name and a quantity to variables, a prompt can show each detail on its own line:
Sub ShowOrderDetails()
Dim productName As String
Dim quantity As Long
Dim messageText As String
productName = "Notebook"
quantity = 3
messageText = "Product: " & productName & vbCrLf & _
"Quantity: " & quantity
MsgBox messageText, vbInformation, "Order details"
End Sub
That prompt displays “Product: Notebook” on the first line and “Quantity: 3” on the second. In a workbook macro, the values could instead come from cells, but the line-break technique is unchanged. Assigning the completed prompt to a clearly named string variable also makes it easier to inspect and maintain than putting a long concatenation directly inside the MsgBox call.
Control blank lines and spacing
Use two consecutive newline constants to leave an empty line between sections. One newline moves to the next line; a second one moves past an additional line. This can separate a short status from follow-up instructions, though too many blank lines can make a small dialog awkward to read.
Sub ShowSeparatedMessage()
Dim messageText As String
messageText = "Import complete." & vbCrLf & vbCrLf & _
"Rows added: 25" & vbCrLf & _
"Review the results before closing the workbook."
MsgBox messageText, vbInformation, "Import status"
End Sub
This creates a blank line after the first sentence, then puts the row count and reminder on consecutive lines. To avoid an unwanted blank line at the beginning or end, do not add a newline before the first text or after the final text unless that spacing is deliberate. A trailing break may not be visually obvious in every dialog, so it is generally simpler to omit it.
Line breaks are not code line continuations
A common source of confusion is that VBA source code can span multiple editor lines without displaying those lines in the message. The underscore used at the end of a continued statement only tells VBA that the statement continues on the next source line. It is not a character in the prompt. Conversely, vbCrLf is part of the string expression and creates a break when the prompt is displayed.
VBA does not let you split a quoted string by pressing Enter inside the quotation marks. Instead, close the string, concatenate a newline constant, then concatenate the next string. For instance, the pattern is text, ampersand, newline constant, ampersand, text. This approach also avoids relying on how a particular editor handles pasted or invisible characters.
Do not confuse a newline with vbTab. A tab inserts horizontal spacing; it does not start a new line. Likewise, adding spaces around a phrase only changes its horizontal position and will not create a separate row in the dialog.
Buttons, title, and line breaks are separate
The standard MsgBox syntax accepts a prompt followed by optional button and title arguments. The prompt is where the newline belongs. The buttons argument controls options such as which buttons appear and whether the dialog uses an icon; the title argument supplies the text in the dialog’s title bar. Adding vbCrLf to the prompt does not change the available buttons.
For example, the following call uses a two-line prompt, a question icon, and Yes and No buttons. The result returned by MsgBox is stored so code can respond to the person’s choice:
Sub AskAboutChanges()
Dim answer As VbMsgBoxResult
Dim promptText As String
promptText = "Save the changes?" & vbCrLf & _
"Choose Yes to continue or No to stop."
answer = MsgBox(promptText, vbYesNo + vbQuestion, "Confirm action")
If answer = vbYes Then
MsgBox "Continuing.", vbInformation, "Confirmed"
End If
End Sub
The line break improves the wording, while vbYesNo and vbQuestion determine the dialog’s controls and icon. If an answer matters, capture the return value rather than assuming that showing the dialog automatically pauses the macro’s later decision-making. A MsgBox is modal by default, so the user must dismiss it or make the offered choice before continuing.
Common problems and how to fix them
- The code shows the text “vbCrLf.” Check that the constant is outside the quotation marks. Within quotes it is ordinary text; outside quotes, joined with ampersands, it is a VBA constant.
- The message appears on one line. Verify that the prompt expression includes the newline between the text sections and that the sections are joined with
&. A source-code underscore alone does not affect the displayed prompt. - The code will not compile after adding a line break. Check that each quoted string has a matching closing quote, each concatenation has an ampersand, and any continued source line has a space followed by an underscore at its end. Do not put other code after the continuation underscore.
- There is an unexpected empty line. Look for two newline constants in a row. Use one between ordinary adjacent lines and two only when you want a blank line.
- The dialog is difficult to read. Shorten the prompt, use a descriptive title, and break long instructions into sensible phrases. A MsgBox is a compact dialog, not a substitute for a full report or a large form.
If a message includes values that may be blank, consider whether an empty value should still have its own labelled line. Clear labels such as “Customer:” and “Status:” make a multiline prompt easier to scan and reduce ambiguity. When the output can become very long, displaying it in a worksheet or a custom form is usually more usable than continually adding lines to a MsgBox.
When to use a different approach
MsgBox works well for short notifications, confirmations, and simple summaries. It has limited space and a small set of standard controls, so it is not ideal for lengthy explanations, editable data, or a list that changes in size. A custom VBA UserForm gives you more control over layout and can include text boxes, labels, and buttons. Writing information to worksheet cells is often better when readers need to review, sort, or keep the details after dismissing a dialog.
An InputBox is for collecting a brief response, not for presenting a richly formatted report. A worksheet cell formula is also not a replacement for MsgBox: MsgBox is a VBA statement that opens a dialog when code runs, whereas a formula returns a value in a cell. Choose based on what the user needs to do next, rather than using a dialog just because it can show a newline.
Frequently Asked Questions
How do I start a new line in a VBA MsgBox?
Join the text sections with vbCrLf or vbNewLine, for example: text, ampersand, vbCrLf, ampersand, more text.
Is vbCrLf or vbNewLine better for MsgBox?
Both are suitable for ordinary multiline prompts. vbCrLf explicitly uses a carriage return and line feed and is common in Windows VBA; vbNewLine expresses a platform-appropriate newline.
Can I press Enter inside a VBA string to make a new line?
No. Do not press Enter inside a quoted string to split it. Close the string and concatenate a newline constant, then concatenate the next string.
Why does vbCrLf appear in my message instead of making a line break?
It is probably inside quotation marks, where VBA treats it as literal text. Put the constant outside the quotes and concatenate it between the text sections.
How do I add a blank line to a MsgBox?
Use two newline constants consecutively between the surrounding text. One constant moves to the next line; the second leaves an empty line in between.
Related
- Microsoft Excel Shortcut Keys: Essential Commands
- Microsoft Excel Scenario Manager: Setup, Uses, and Limits
- Microsoft Excel: Remove Page Break Lines and Hide Them
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA: Find the First Non-Empty Row in a ColumnTo find the first non-empty row in an Excel column with VBA, use the range’s Find method with…
- Excel VBA Find and Replace: Methods and ExamplesTo find and replace text with Excel VBA, use the Range.Replace method and specify what to search for,…
- Excel VBA: Get the Day of the Week from a Date ValueIn Excel VBA, use Weekday to get a date’s weekday number and WeekdayName to turn that number into…
- Excel VBA: Copy the End Part of a String with RightTo copy the last characters of a string in Excel VBA, use the Right$ function: assign Right$(text, numberOfCharacters)…
- Excel Macros in Google Sheets: What Works?No: Google Sheets cannot run Excel VBA macros, even when you open an Excel workbook that contains them.…
- Excel Macro for Multiple-Selection Drop-Down ListsTo let a user choose several items from one Excel drop-down, add a VBA Worksheet_Change macro to the…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.