Office Settings & Configuration

Fix SetFocus After a Message Box in a Word VBA UserForm

Steven ParkerSteven Parker Nov 18, 2026 869 views

Question details

Keep focus in a Word VBA UserForm text box when a user enters an invalid time.

Fix SetFocus After a Message Box in a Word VBA UserForm
Product
Microsoft Word VBA UserForm
Device & OS
not provided
Scenario
A UserForm validates a time entry when the user moves to another control.
Observed behavior
After a message box appears, focus moves to the next text box even though SetFocus is called in the AfterUpdate event.
Before you start

Open the UserForm in Word’s VBA editor and identify the name of the time-entry text box. Check its existing AfterUpdate code so you can move the validation without displaying duplicate messages.

Solution 1Recommended

Validate the Time Entry in the Text Box’s Exit Event

Cancel the text box’s Exit event when validation fails to keep the user in the control.

The Exit event lets you cancel the attempted focus change. This is more reliable for keeping the user in the text box than calling SetFocus from AfterUpdate.

1
Open the UserForm code

In desktop Word, open Developer > Visual Basic. In the Project Explorer, open your UserForm and its code window.

2
Add an Exit event

Select the time-entry text box in the code window’s object list, then select Exit in the procedure list. Replace txtTime in the following example with your text box’s actual name: Private Sub txtTime_Exit(ByVal Cancel As MSForms.ReturnBoolean) If Not IsDate(Me.txtTime.Value) Then MsgBox "Enter a valid time.", vbExclamation Cancel = True Me.txtTime.SelStart = 0 Me.txtTime.SelLength = Len(Me.txtTime.Text) End If End Sub

3
Remove duplicate validation

In the same code window, remove or disable the equivalent validation in txtTime_AfterUpdate so it does not show a separate message or attempt to move focus.

4
Test the UserForm

Run the UserForm, enter an invalid value in the time box, and press Tab. Dismiss the message and confirm that the entry remains selected in the original text box.

Validate the Time Entry in the Text Box’s Exit Event
Choose the validation rule you need: IsDate accepts recognizable dates as well as times, according to the system’s regional settings. If the field must accept only a particular time format, replace that condition with a stricter validation rule.
Free Microsoft Office alternative

Edit Word Documents with WPS Office

WPS Office is a free, lightweight alternative for editing Word documents, with a familiar interface and Microsoft Office format support to help you move existing files. It does not fix Microsoft Word’s VBA UserForm event behavior; macro compatibility may vary.

Edit documents in a familiar word-processing interfaceOpen and save Microsoft Word DOC and DOCX filesUse a free, lightweight office suite for everyday document workMove existing documents to WPS without recreating their contents
microsoft office alternative - wps office

Frequently Asked Questions

Why does SetFocus in AfterUpdate not keep the text box active?

AfterUpdate runs after the control’s value changes, but the pending focus change can still continue. Use the text box’s Exit event and set Cancel = True when the entry is invalid.

Do I need to call SetFocus in the Exit event?

No. Setting Cancel = True in the text box’s Exit event prevents focus from leaving it. You can then set SelStart and SelLength to select the invalid entry.

How do I select the invalid value so the user can replace it?

After setting Cancel = True, set the text box’s SelStart to 0 and its SelLength to Len of its Text property, as shown in the Exit event example.

Does IsDate accept only time values?

No. IsDate can accept both dates and times, and recognition depends on regional settings. Use a stricter validation rule in the Exit event if the field requires a specific time-only format.