To put a carriage return and line break in a VBA MsgBox, join the message parts with vbCrLf: for example, MsgBox "First line" & vbCrLf & "Second line". In Excel for Microsoft 365 on Windows, vbNewLine is another readable option for starting a new line in the prompt.
What a carriage return does in a MsgBox
A MsgBox displays the text supplied as its prompt. A carriage return moves the text position to the start of a line; a line feed advances to the next line. The traditional Windows line break combines both characters, in that order. VBA provides named constants so you can add that combination without typing invisible control characters.
The usual choice is vbCrLf, which represents a carriage return followed by a line feed. When VBA builds the prompt, the constant becomes part of the text passed to the dialog, and the dialog renders the later words on a new line. The line break is inside the prompt; it does not create a second dialog or change the buttons.
It helps to distinguish this from pressing Enter while editing code. Enter starts a new line in the Visual Basic Editor, but it does not automatically insert a line break into a string. To make a break appear in the message, concatenate a line-break constant into that string.
The recommended method: vbCrLf
Use the ampersand operator to join the text before and after the break. This complete procedure displays a two-line message with an information icon and a custom title:
Sub ShowTwoLineMessage()
' Build a prompt with a Windows-style line break.
Dim prompt As String
prompt = "The report is ready." & vbCrLf & "You can review it now."
MsgBox prompt, vbInformation, "Report status"
End Sub
Here, vbInformation selects the information icon, and "Report status" supplies the dialog title. Neither is required for the line break; the essential part is & vbCrLf &. If you want the default buttons and icon, you can simply write MsgBox prompt.
For a message assembled from several pieces, add a break at each point where the next text should begin a new line. For example, a status prompt could put a label and its value on separate lines, or show a short heading followed by an explanation. Each occurrence of vbCrLf adds another line boundary.
Other VBA line-break constants
VBA includes several constants that may look interchangeable, but they represent different characters:
vbCrLfis carriage return plus line feed. It is the conventional Windows line ending and a dependable choice for a Windows MsgBox.vbNewLinerepresents a newline. It is often useful when you want code that expresses the general idea of a new line rather than naming its characters.vbCris a carriage return alone.vbLfis a line feed alone.
For ordinary messages in Excel for Windows, start with vbCrLf. vbNewLine is also appropriate for a normal line break and can make the intent especially clear. A single vbCr or vbLf may render as a break in some contexts, but using the established pair avoids relying on how a particular text display handles only half of a Windows line ending.
These are VBA constants, not Excel worksheet functions. Use them in VBA code, such as a macro or an event procedure, rather than entering vbCrLf into a worksheet cell and expecting Excel to interpret it as a formula function.
Build a message from variables
Line breaks are particularly useful when a prompt includes values calculated or read by a macro. Construct the full message as a string, placing vbCrLf between the pieces that should appear on separate lines. This example shows the pattern; replace the sample values with the values your procedure actually obtains.
Sub ShowOrderSummary()
' Example values; replace these with workbook data as needed.
Dim customerName As String
Dim orderCount As Long
Dim prompt As String
customerName = "Jordan Lee"
orderCount = 3
prompt = "Customer: " & customerName & vbCrLf & _
"Open orders: " & CStr(orderCount)
MsgBox prompt, vbInformation, "Order summary"
End Sub
The underscore at the end of the first assignment line is VBA’s explicit line-continuation character. It lets the source code continue on the next editor line; it is separate from vbCrLf, which creates the visible break in the dialog. The underscore must be preceded by a space and must be the last character on that code line. If you remove the underscore and split the assignment across editor lines, the code will not compile as written.
Control spacing and blank lines
One line-break constant starts the next line. To leave an empty line between two blocks of text, use two breaks in succession. For example, the pattern "Summary" & vbCrLf & vbCrLf & "Details" puts a blank line between the heading and the details. This is useful for separating a warning from an explanation, but too many blank lines can make a compact dialog harder to scan.
You can also put a break at the beginning or end of the prompt, but that usually creates unnecessary empty space above or below the text. Prefer breaks between meaningful parts. For longer prompts, consider whether the message is still easy to read in a standard dialog; a MsgBox is intended for brief notifications, not for displaying a report or a large block of instructions.
Make sure the text is joined with &. The plus sign can sometimes coerce values in VBA expressions, but it is not the clear string-concatenation operator to use for this job. Using ampersands around each constant and text segment makes the intended operation explicit.
Common reasons the line break does not appear
If the dialog shows the characters vbCrLf instead of breaking the line, inspect how the prompt is built. A constant must be outside the quotation marks so VBA evaluates it. In "First line vbCrLf Second line", everything between the quotation marks is literal text. The working form is "First line" & vbCrLf & "Second line".
Other common issues include:
- Missing concatenation: Put
&between the first string, the constant, and the next string. - Using a worksheet formula idea in VBA:
CHAR(10)is commonly used in worksheet formulas for line breaks, but it is not the normal VBA constant syntax. For a VBA MsgBox, usevbCrLforvbNewLine. - Confusing source-code lines with message lines: Moving part of the code to the next line in the editor does not add a break to the prompt. Add the constant to the string.
- Building the prompt in multiple assignments: Check that later assignments do not overwrite the string created earlier. Use concatenation, such as
prompt = prompt & vbCrLf & "More text", when appending. - Expecting the same behavior in every Excel environment: Excel for the web does not run VBA macros. Open the workbook in the desktop Excel application to run a VBA MsgBox.
If a dialog looks crowded, the problem may not be the line-ending constant. Shorten the message, split it into a few readable lines, or reconsider whether a dialog is the right way to present the information.
MsgBox line breaks versus worksheet line breaks
A VBA MsgBox and a cell use different display contexts. In a MsgBox, add a VBA string constant such as vbCrLf to the prompt. For a worksheet cell, a line break is stored in the cell text; Excel formulas often create one with CHAR(10), and users can insert one while editing a cell with Alt+Enter on Windows. The cell may also need Wrap Text enabled to show multiple lines clearly.
That distinction explains a frequent source of confusion: code copied from a worksheet formula may use CHAR(10), while a VBA message is more naturally written with vbCrLf. They solve related display problems, but they are not the same syntax or used in the same place. If you are building a string in VBA specifically for a MsgBox, prefer the named VBA constant.
Running and saving VBA safely
To try a procedure, use the desktop Excel Visual Basic Editor, insert the code in a standard module, and run the macro. VBA macros do not run in Excel for the web. On Windows, a workbook containing macros should be saved in a macro-enabled format such as .xlsm if you need to retain its VBA project. Excel for Mac also supports VBA in the desktop application, though some Excel features and interface details differ; the line-break constants shown here are VBA string constants, not a Windows-only worksheet feature.
Before running a macro that changes workbook data, save a copy of the workbook. Running a macro cannot be undone with Ctrl+Z, even if the macro also displays a MsgBox. The examples above only display text, but a real procedure may perform other actions before or after the dialog appears, so check the full procedure before running it.
Frequently Asked Questions
What is the VBA code for a new line in MsgBox?
Join the text with vbCrLf, as in MsgBox "First" & vbCrLf & "Second". You can also use vbNewLine for a newline.
Is vbCrLf better than vbNewLine in VBA?
For a Windows Excel MsgBox, both are suitable for a normal new line. vbCrLf explicitly identifies the carriage-return/line-feed pair and is a common, clear choice; vbNewLine expresses the intent more generally.
Can I press Enter inside a VBA string to make a new line?
No. Pressing Enter in the editor moves the code to another source line; it does not insert a line break into a quoted string. Put vbCrLf between string parts instead.
How do I add a blank line to a MsgBox?
Use two line breaks in a row between the surrounding text: "Heading" & vbCrLf & vbCrLf & "Explanation". That leaves one empty displayed line.
Why does my MsgBox show vbCrLf as text?
The constant is probably inside the quotation marks. Close the string before the constant, join it with &, and then concatenate the next quoted string.
Does VBA MsgBox work in Excel for the web?
No. Excel for the web does not execute VBA macros. Open the workbook in desktop Excel to run a procedure that displays a MsgBox.
Related
- Conditional Formatting: Use More Than One Rule in Excel
- table() in RStudio: How It Counts and Displays Data
- Data Validation and Reconciliation in Excel Explained
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Assign Range to a Variable: Examples and TipsTo assign an Excel range to a VBA variable, declare the variable as a Range and use Set:…
- VBA InputBox: How to Collect Multiple InputsVBA’s standard InputBox collects one response per prompt; it does not display several separate fields in a single…
- Excel Macro to Hide Columns: VBA Examples and TipsTo hide columns with a VBA macro in Excel for Microsoft 365 on Windows, put Columns("B:D").Hidden = True…
- Replace Excel Macros? Alternatives and When to SwitchYes—many Excel macros can be replaced, but the right substitute depends on the task: use formulas for calculations,…
- Sending Emails With VBA: Excel and Outlook GuideYes—Excel VBA can create and send email through classic Outlook for Windows. In Excel, add a macro that…
- VBA Remove Item From Dictionary: Methods and ExamplesTo remove one entry from a VBA dictionary, call dict.Remove key, where key is the entry’s key—not its…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.