VBA & Macro Problems

Microsoft Office VBA: Keep Focus on an Invalid UserForm TextBox

Camila MilosovichCamila Milosovich Nov 22, 2026 868 views

Question details

Keep the user's focus in a UserForm TextBox when its value fails VBA validation.

Keep Focus on an Invalid TextBox in a VBA UserForm
Product
Microsoft Office VBA UserForms
Device & OS
not provided
Scenario
A VBA UserForm validates a TextBox value as the user moves to another control.
Observed behavior
After validation rejects the value, focus may move to the next control instead of staying in the invalid TextBox.
Before you start

Open the Office file containing the UserForm and identify the TextBox and its validation rule. In the VBA editor, check whether validation already runs in an event that may conflict with the TextBox Exit event.

Solution 1Recommended

Cancel the TextBox Exit event when validation fails

Use the Exit event to prevent focus from leaving the TextBox until the user corrects an invalid value.

1
Open the UserForm code

Open the Visual Basic editor, locate the UserForm in Project Explorer, and open its code window.

2
Select the Exit event

In the code window, select the TextBox from the object list and Exit from the event list.

3
Add the validation

In the generated Exit procedure, set Cancel to True when the value is invalid. For example, replace TextBox1 with your control's name and use: Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) If Len(Trim$(Me.TextBox1.Value)) = 0 Then MsgBox "Enter a value before continuing." Cancel = True End If End Sub

4
Test the form

Run the UserForm, leave the TextBox empty, and press Tab or click another control. After dismissing the message, enter a valid value and try moving on again.

Cancel the TextBox Exit event when validation fails
Focus and selection: Canceling the Exit event prevents focus from moving away, but it does not automatically select the text in the TextBox. Replace the example's empty-value check with your actual validation rule.
Free Microsoft Office alternative

Work with Office spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for working with Office files. Open compatible spreadsheets and migrate everyday editing tasks with ease; it does not fix Microsoft Office VBA UserForm focus behavior.

Edit spreadsheets in a familiar interfaceOpen and save common Microsoft Excel formats, including XLSXUse spreadsheet data validation for suitable cell-entry workflowsMove everyday document and spreadsheet work to a lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does focus move to the next control after validation fails?

Displaying an error message alone does not cancel a focus change. In the TextBox Exit event, set Cancel = True when the value is invalid.

Does Cancel = True select the invalid text?

No. It prevents the TextBox from losing focus, but it does not automatically select its contents.

Where can I find the TextBox Exit event?

Open the UserForm's code window in the Visual Basic editor, select the TextBox from the object list, and then select Exit from the event list.

Can a modeless UserForm affect focus behavior?

It can contribute to inconsistent focus behavior. Check the UserForm's ShowModal property and the code calling Show; an explicit vbModeless argument displays the form modelessly even if ShowModal is True.