Excel VBA UserForm TextBox Properties Guide
In this article
To set Excel VBA UserForm TextBox properties, open the UserForm in the Visual Basic Editor, select the TextBox, then edit its properties in the Properties window (F4) or in code with TextBox1.PropertyName. The most important properties are Name, Text, MaxLength, MultiLine, PasswordChar, Enabled, Visible, Locked, TabIndex, ControlTipText, and the Change, Enter, Exit, and KeyPress events. This guide walks through the practical ones, the mistakes people make, and how to fix them.
What You'll Need
- Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
- A workbook saved as .xlsm (macro-enabled) so your UserForm and code persist.
- Basic familiarity with the Visual Basic Editor (Alt+F11) and the Properties window (F4).
Menu labels can differ slightly by Excel version, device, or region, but the VBA Editor and Properties window are consistent across desktop versions. These steps are for Excel on Windows and Mac desktop; the VBA Editor is not available in Excel for the web.
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or the Developer tab > Visual Basic). In the Project Explorer, right-click your workbook and choose Insert > UserForm.
- Add a TextBox. From the Toolbox (View > Toolbox if hidden), click the TextBox control and drag it onto the UserForm. Repeat for each text box you need.
- Open the Properties window. Select the TextBox and press F4. The Properties window lists properties alphabetically; the top section shows the object name (e.g., TextBox1) and the dropdown lets you switch between controls.
- Set the Name property. Change
Nameto something meaningful liketxtFirstName. This is the name you use in code, so avoid spaces and reserved words. Renaming now saves confusion later. - Set the Text property. Use
Textto set the default value shown when the form opens. In code,txtFirstName.Text = ""clears it;txtFirstName.Valueis equivalent for most purposes. - Limit input with MaxLength. Set
MaxLengthto a number (e.g., 50) to cap characters. Leave it 0 for unlimited. This is a simple guard against overly long entries. - Enable multi-line input. Set
MultiLineto True andWordWrapto True if you want a notes field. MultiLine only works whenEnterKeyBehavioris set appropriately; otherwise Enter submits the form. - Mask passwords. Set
PasswordCharto a character like*. The typed text is hidden but theTextproperty still returns the real value, so validate it in code. - Control availability. Set
Enabledto False to grey out a text box, orVisibleto False to hide it. UseLockedto allow selection but prevent editing. - Set tab order. Use
TabIndexto control the order users tab through fields. Start at 0 for the first field. Also setTabStopto False for fields users should skip. - Add helpful hints. Set
ControlTipTextto a short hint that appears on hover. UseToolTipTextsimilarly if needed. Keep hints brief. - Handle events. Double-click the TextBox to open the code window. Use
Changefor live validation,EnterandExitfor focus changes, andKeyPressto restrict characters (e.g., digits only). Example:If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0blocks non-digits. - Validate on submit. In the OK button's Click event, check each text box's
TextorValueand show aMsgBoxif invalid. Do not rely on the user to follow hints. - Test the form. Press F5 in the UserForm to run it. Enter data, tab through fields, and check that defaults, limits, and masks behave as expected. Close and adjust properties as needed.
Common Mistakes
- Using the default names. TextBox1, TextBox2, etc., become unreadable in code. Rename controls early.
- Confusing Text and Value. For a TextBox they usually match, but
Textreflects what is displayed; useValuewhen reading user input consistently. - Forgetting MultiLine needs EnterKeyBehavior. Without it, pressing Enter triggers the default button instead of a new line.
- Relying on PasswordChar for security. It only masks display; the underlying text is still plain in memory. Do not store passwords insecurely.
- Setting properties in the wrong place. Properties set in the Properties window are design-time defaults; changing them in code at runtime overrides them until the form is unloaded.
- Ignoring TabIndex. Users tab in creation order, which may not match the visual layout. Set TabIndex deliberately.
Tips
- Use a naming convention like
txtprefix for text boxes,btnfor buttons,lblfor labels. - Set
MaxLengthfor fields like phone numbers or postal codes to prevent accidental long entries. - Use
ControlTipTextto explain expected formats without cluttering the form. - Validate in the
Exitevent to catch errors as the user leaves a field, and again on submit. - Use
SelStartandSelLengthto position the cursor or select text programmatically. - Keep the Properties window open while designing; it speeds up tweaking multiple controls.
Troubleshooting
If a property change does not seem to take effect, check whether code is overriding it at runtime. If the form does not run, ensure macros are enabled and the workbook is saved as .xlsm. If text boxes are not visible, check Visible and whether they are behind other controls. If tab order is wrong, review TabIndex and TabStop. If input is not limited, confirm MaxLength and any KeyPress code are correct. If the form crashes on open, look for invalid property values (e.g., negative MaxLength) and reset them in the Properties window. For persistent issues, export the form, create a fresh one, and copy controls over one at a time to isolate the problem.
Frequently Asked Questions
How do I clear all text boxes when the form opens?
In the UserForm's Initialize event, set each text box's Text property to an empty string, e.g., txtFirstName.Text = "".
Can I make a TextBox accept only numbers?
Yes. Use the KeyPress event to block non-digit characters by setting KeyAscii = 0 when the key is outside 48–57. Also handle paste separately if needed.
Why does my MultiLine TextBox not accept Enter?
Set MultiLine to True and EnterKeyBehavior to True. Without the latter, Enter triggers the default button.
How do I change the font of a TextBox?
Select the TextBox, open the Properties window, and set the Font property. You can also set it in code with txtFirstName.Font.Name = "Arial".
What is the difference between Enabled and Locked?
Enabled = False greys out the control and prevents focus. Locked = True allows focus and selection but prevents editing.
Can I set a default value that users can overwrite?
Yes. Set the Text property in the Properties window or in the Initialize event. Users can edit it unless Locked is True.