This guide shows how to insert a line break into a cell or message box using VBA. The most direct method is adding vbCrLf to your string, which forces Excel to start a new line.
What You'll Need
- Microsoft Excel installed on a Windows computer (VBA is not available on the Mac version of Excel without significant workarounds).
- Basic knowledge of the Excel interface, specifically knowing how to open the VBA Editor.
Step-by-Step
- Open the VBA Editor.
- Insert a New Module.
- Define a Sub Procedure.
- Create a Variable for the Text.
- Concatenate Text with the Line Break Code.
- Assign the String to a Range or Message Box.
- Run the Code.
What You'll Need
To follow these steps, you need access to the VBA development environment. While no physical tools are required, the software environment is essential.
- Microsoft Excel (Windows). The Visual Basic for Applications (VBA) editor is a standard component of the Windows version of Excel. It is not included in the standalone versions for macOS or the web version of Excel. You must be working in a desktop application installed on a Windows operating system to use this method.
- Basic familiarity with the Excel Interface. You do not need to be a programmer, but you should be comfortable navigating the ribbon and knowing where the Developer tab is located. If you cannot find the Developer tab, you can enable it in Excel Options.
Step-by-Step
Performing a line break in VBA is a two-part process. First, you must construct the string containing the text you want to display. Second, you must use the specific VBA constant that represents a line break to tell Excel where to move to the next line.
The core of this task is the vbCrLf constant. This stands for Visual Basic Carriage Return and Line Feed. In simpler terms, it is the keyboard equivalent of pressing the Enter key while typing. When VBA encounters this code inside a text string, it stops the current line of text and begins a new one.
There are two primary ways to use this line break: writing it to a cell on a spreadsheet or displaying it in a popup message box. The code logic is identical; the difference is how the variable is assigned.
Method 1: Inserting a Line Break into a Cell
This method is useful when you want to create a report or a data entry form where the text is permanently stored in a worksheet.
- Open the VBA Editor.
- Insert a New Module.
- Define a Sub Procedure.
- Create a Variable for the Text.
- Concatenate Text with the Line Break Code.
- Assign the String to a Range or Message Box.
- Run the Code.
Step-by-Step
-
Open the VBA Editor.
Start by launching Excel. On the ribbon at the top of the screen, look for the Developer tab. If you do not see this tab, go to File > Options > Customize Ribbon and check the box for Developer in the list of main tabs. Once visible, click on the Developer tab and select Visual Basic from the code group on the left side. This will open the VBA Editor window.
-
Insert a New Module.
In the VBA Editor, you will see a window titled something like VBAProject (Your Workbook Name). This is the project explorer. To add a new code module, right-click anywhere inside this project explorer. In the context menu that appears, select Insert and then Module. A new, blank code window will appear on the right side of the editor.
-
Define a Sub Procedure.
Click into the blank white space of your new module. You need to tell VBA that you are about to write a program. Type
Sub InsertLineBreak(). This name is arbitrary, but it must follow naming conventions (no spaces, start with a letter). Press the Enter key on your keyboard. VBA will automatically close the line with anEnd Substatement. -
Create a Variable for the Text.
Inside the
Sub InsertLineBreak()block, you will write your code. It is good practice to declare a variable to hold your text. TypeDim myText As String. This line of code creates a storage space namedmyTextthat is capable of holding text strings. -
Concatenate Text with the Line Break Code.
This is the most critical step. You will now assign your text to the variable. Type
myText = "This is the first line." & vbCrLf & "This is the second line.". Let's break down the syntax."This is the first line.": This is the literal text you want to appear on the first line.&: This is the concatenation operator. It tells VBA to join the text on the left with the text on the right.vbCrLf: This is the VBA constant for a line break."This is the second line.": This is the text you want to appear on the next line.
When you run this code, VBA will take the first sentence, insert a new line, and then append the second sentence.
-
Assign the String to a Range or Message Box.
Now that you have constructed the string, you must decide where to put it. You have two main options.
Option A: Display in a Message Box.
Replace the
myText = ...line withMsgBox myText. This will display a popup window in Excel showing your two lines of text.Option B: Insert into a Cell.
If you want the text to appear inside a spreadsheet cell, you need to reference a specific cell. For this example, we will write the text to cell A1. Type
Range("A1").Value = myText. This will permanently write the line break into the cell on your worksheet. -
Run the Code.
Click anywhere inside the code block. Press the green Play button located on the toolbar of the VBA Editor (or press F5 on your keyboard). The message box will appear, or cell A1 will be updated with the new text. You can verify the result by clicking on cell A1 and pressing Alt + Enter to see if the cursor moves to a new line.
Method 2: Using the Excel Text Editor
While vbCrLf is the programmatic way to do it, you can also achieve the same result visually using the standard Excel text editor. This is useful if you are building a string manually without writing code.
-
Double-click on a cell.
Navigate to the cell where you want to enter the text. Double-click the left mouse button to enter edit mode.
-
Type your first line of text.
Type the text you want to appear on the first line.
-
Press Alt + Enter.
This is the standard keyboard shortcut for inserting a line break directly into a cell. It works in the formula bar as well as inside the cell itself.
-
Type your second line of text.
Type the text you want to appear on the next line. The text will wrap to the next line within the cell boundaries.
-
Exit Edit Mode.
Press Enter or click away from the cell to save your changes.
Method 3: Using the CHAR Function
Another way to create a line break is by using the Excel worksheet function CHAR. This method is useful if you are constructing a string using formulas rather than VBA code.
-
Use the CHAR function.
Type the following formula into a cell:
="This is the first line." & CHAR(10) & "This is the second line.". -
Enable Wrap Text.
By default, Excel will not wrap the text even though you used the line break character. You must format the cell to enable wrapping. Select the cell, go to the Home tab on the ribbon, and click the Wrap Text button.
-
Verify the result.
The text will now display on two separate lines within the cell.
Common Mistakes
When working with line breaks in Excel VBA, a few common errors can prevent the text from displaying correctly.
Mismatched Quotes
The most frequent error is a typo in the quotation marks. The string must be enclosed in double quotes ("). If you accidentally use a single quote (') instead, VBA will treat the rest of the line as a comment and ignore it.
Missing the Ampersand (&)
When joining two strings, you must use the ampersand operator (&). Forgetting this operator will result in a runtime error. The code text1 & vbCrLf & text2 is correct, whereas text1 vbCrLf text2 will not work.
Not Formatting the Cell
If you are using the CHAR(10) method or the visual Alt + Enter method, the cell must be formatted to wrap text. If the cell is not formatted, the text will appear as one long line. This is a common point of confusion because the data is technically correct, but the display is incorrect.
Using the Wrong Constant
VBA has several constants for line breaks. vbCrLf is the standard Windows line break, which usually works across different systems. However, you might sometimes see vbCr (Carriage Return only) or vbLf (Line Feed only). These are less common and may behave differently depending on how the text is being processed by the receiving application.
Tips
Use Constants for Readability
Always use vbCrLf in your code. It is much more readable than typing the ASCII code or the CHAR function. It also makes your code easier to maintain if you change the text later.
Check for Null Values
If your text comes from a variable that might be empty, you should check for it before adding the line break. This prevents errors like Run-time error '94': Invalid use of Null. You can use the IsNothing function or the IsNumeric function to check the variable.
Limit Line Lengths
When displaying text in a message box, be mindful of the line length. Long lines of text can make it difficult to read the message. You can use the vbCrLf to force breaks in the middle of a sentence if it gets too long.
Use the Immediate Window for Debugging
If you are unsure if your line break is working, you can print the string to the Immediate Window. Press Ctrl + G to open the Immediate Window. Then, type Debug.Print myText. This will display the string in the VBA Editor's output window.
Troubleshooting
If your code does not produce the expected result, check the following common issues.
Text Appears as One Long Line
If you are using vbCrLf in a message box, it should always work. However, if you are using CHAR(10) in a cell, you must ensure that the cell is formatted with Wrap Text. Go to the Home tab on the ribbon and click the Wrap Text button.
Runtime Error 13: Type Mismatch
This error occurs when you try to join a string with a number. For example, myText = "Line 1" & vbCrLf & 123. You must convert the number to a string using the CStr function: myText = "Line 1" & vbCrLf & CStr(123).
Text is Truncated
If the text is being cut off, it may be because the cell width is too narrow. Increase the width of the column to accommodate the text. You can do this by hovering your mouse over the vertical line between the column headers until the cursor changes to a double-headed arrow, and then double-clicking.
Frequently Asked Questions
Can I use a line break in a formula?
Yes, you can use the CHAR(10) function inside a standard Excel formula. However, you must select the cell and apply the Wrap Text formatting to the cell for the text to wrap to the next line.
Does vbCrLf work in all versions of Excel?
vbCrLf is a standard VBA constant that has been available in Excel for Windows for many years. It is consistently supported across all modern versions of the software.
How do I insert a line break in a VBA message box?
You insert a line break in a message box by adding the & vbCrLf & operator between the parts of your text string before passing it to the MsgBox function.
Can I use a line break in a chart title?
Yes, you can insert a line break in a chart title by editing the title in the Format Data Labels or Format Axis pane. Click on the title, press Alt + Enter, and type the next line of text.
Why is my text not wrapping in a cell?
The most common reason is that the Wrap Text formatting is not applied. Select the cell and click the Wrap Text button in the Home tab on the ribbon. Alternatively, the column width may be too narrow to accommodate the text.