Introduction
In VBA UserForms, there are times when you want users to input free-form text across multiple lines, such as for addresses or remarks. While the default TextBox only accepts a single line, you can easily support multi-line input by setting a few specific properties.
In this article, I will explain the key properties required to enable line breaks and multi-line display in a TextBox, along with a clear code example.
Sample Code: Multi-Line TextBox Settings
Settings related to multi-line input are typically handled within the UserForm_Initialize event, which runs just before the form appears.
In this example, we will configure a TextBox named Remarks_TextBox (placed on a form named MemoForm) to function as a multi-line remarks field.
UserForm Code
' Event executed when the form is initialized
Private Sub UserForm_Initialize()
With Me.Remarks_TextBox
' .MultiLine property: Enable multi-line input
.MultiLine = True
' .WordWrap property: Enable automatic wrapping at the right edge
.WordWrap = True
' .EnterKeyBehavior property: Enable line breaks with the Enter key
.EnterKeyBehavior = True
' .ScrollBars property: Show scrollbars as needed
.ScrollBars = fmScrollBarsVertical
' Set initial value (Insert line break with vbCrLf)
.Text = "[Special Notes]" & vbCrLf & "1. "
End With
End Sub
Preparation Before Execution
- Create a UserForm named
MemoForm. - Place a TextBox on the form and resize it vertically to accommodate multiple lines.
- Change the (Name) property of the TextBox to
Remarks_TextBox. - Paste the code above into the form’s code module. When you run the form, the TextBox will appear with multi-line settings applied.
Explanation of Code (Properties)
.MultiLine = True
This is the most fundamental property for this task. Setting this to True allows the TextBox to handle multiple lines of text. If this is not set, the other properties below will not function as intended.
.WordWrap = True
This setting determines whether text automatically wraps to the next line when it reaches the right edge of the TextBox. If True, long sentences will automatically stay within the box width. It is usually convenient to set this to True.
.EnterKeyBehavior = True
This controls the behavior when the user presses the Enter key inside the TextBox.
- True: A new line is inserted within the TextBox. This is the natural behavior for multi-line input fields.
- False: Pressing Enter moves the focus to the next control in the tab order.
.ScrollBars
This sets whether to display scrollbars when the text exceeds the visible area of the TextBox.
fmScrollBarsNone: No scrollbars (Default).fmScrollBarsHorizontal: Horizontal scrollbar.fmScrollBarsVertical: Vertical scrollbar.fmScrollBarsBoth: Both horizontal and vertical.
When MultiLine = True, it is user-friendly to set this to fmScrollBarsVertical so users can navigate through long text.
vbCrLf
If you want to include line breaks within a string in your VBA code, use the constant vbCrLf. By concatenating strings like "Line 1" & vbCrLf & "Line 2", you can set multi-line text as the initial value of the TextBox.
Summary
In this article, I explained the settings required to make a UserForm TextBox support multi-line input.
- MultiLine = True: The absolute prerequisite.
- WordWrap = True: Enables automatic text wrapping.
- EnterKeyBehavior = True: Allows creating new lines with the Enter key.
- ScrollBars: Adds scrollbars when necessary.
By combining these properties, you can create user-friendly remarks fields or comment boxes on your forms where users can type freely.
