Others

Validate a YYYY-N File Number in a Word VBA UserForm

Tauseeq MagsiTauseeq Magsi Nov 14, 2026 869 views

Question details

Validate a Word UserForm file number consisting of a four-digit year, a hyphen, and a one- to six-digit number, without allowing future years.

Product
Microsoft Word VBA UserForm
Device & OS
not provided
Scenario
A user enters a file number in a UserForm text box before continuing with the form.
Observed behavior
The form should accept entries such as 2024-123 and reject malformed entries or years later than the current year.
Before you start

Open the document containing the UserForm in Word and confirm that macros are enabled for the file. In the VBA editor, check the text box's Name property; the example below uses txtFileNumber.

Solution 1Recommended

Validate the file number in the text box Exit event

Use this approach to check the format and year when the user moves focus away from the text box.

The Exit event cancels the focus change when the entry is invalid. It checks the year against the computer's current date and accepts one to six digits after the hyphen.

1
Open the UserForm code

In Word, press Alt+F11 to open the VBA editor. In Project Explorer, open your UserForm, select its file-number text box, and confirm its Name property is txtFileNumber or adapt the code to match.

2
Add the Exit event

In the UserForm code window, select txtFileNumber from the object list and Exit from the event list. Replace the generated event procedure with: Private Sub txtFileNumber_Exit(ByVal Cancel As MSForms.ReturnBoolean) Dim entry As String Dim yearPart As String Dim numberPart As String Dim valid As Boolean entry = txtFileNumber.Text valid = False If Len(entry) >= 6 And Len(entry) <= 11 Then yearPart = Left$(entry, 4) numberPart = Mid$(entry, 6) If yearPart Like "####" And Mid$(entry, 5, 1) = "-" Then If Not numberPart Like "*[!0-9]*" Then If CInt(yearPart) <= Year(Date) Then valid = True End If End If End If If Not valid Then MsgBox "Enter a file number in YYYY-N format with a non-future year and 1 to 6 digits after the hyphen.", vbExclamation Cancel = True End If End Sub

3
Test the form

Run the UserForm from the VBA editor and enter a valid value such as 2024-123. Move to another control, then test a future year, a missing hyphen, and a number with seven digits.

Validate the file number in the text box Exit event
Also check submission: The Exit event checks the entry when focus leaves the text box; it may not cover every way a form can be submitted or closed. Validate again in the submit button's Click procedure if the value must always be checked.
Free Microsoft Office alternative

Work with Word documents in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents. Its format support helps you move everyday documents between WPS and Microsoft Office; Word VBA UserForms may require separate compatibility testing.

Open and edit Microsoft Word DOC and DOCX filesUse familiar document-editing tools for everyday workMove common Word documents into a lightweight office suiteReview documents that contain file-number fields
microsoft office alternative - wps office

Frequently Asked Questions

Where should I put the validation code?

Open the UserForm code window in the VBA editor and add the procedure to the file-number text box's Exit event. Rename the procedure and text box references if your control is not named txtFileNumber.

How does the code reject a future year?

After checking the four-digit year, it compares CInt(yearPart) with Year(Date), which uses the computer's current date.

Will the code accept leading zeros in the file number?

Yes. The portion after the hyphen is treated as text, so an entry such as 2024-001 is accepted if the year is not in the future.

Does the Exit event guarantee validation when the form is submitted?

Not in every form workflow. Add the same validation to the submit button's Click procedure if submission must always check the value.