Fix SetFocus After a Message Box in a Word VBA UserForm
Question details
Keep focus in a Word VBA UserForm text box when a user enters an invalid time.

- 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.
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.
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.
In desktop Word, open Developer > Visual Basic. In the Project Explorer, open your UserForm and its code window.
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
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.
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.

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.

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.




