Validate YYYYMMDD and YYYY-MM-DD Dates in WPS Writer VBA UserForms
Question details
The user needs a Word-compatible VBA UserForm to accept date input in either YYYY-MM-DD or YYYYMMDD format and convert it to a consistent YYYY-MM-DD date value.

- Product
- WPS Writer
- Device & OS
- not provided
- Scenario
- The user is building or editing a VBA UserForm in WPS Writer and needs to validate manually typed date values before saving them.
- Observed behavior
- The input can arrive as either YYYYMMDD or YYYY-MM-DD and must be validated, then stored consistently as YYYY-MM-DD.
Open the VBA editor in WPS Writer with Alt+F11 and confirm that the UserForm contains a text box for date input and a command button for submission. Enable macros for the document so the UserForm can run during testing.
Validate and normalize YYYYMMDD or YYYY-MM-DD input with VBA code
Use this solution when the UserForm receives dates in either hyphenated or unhyphenated format and must store a consistent YYYY-MM-DD value.
The code removes hyphens, splits the input into year, month, and day parts, builds a real date with DateSerial, and only returns the formatted result if the constructed date is valid. This prevents invalid strings from being inserted into the form or document.
In WPS Writer, press Alt+F11 to open the VBA editor, then insert a standard module in the project where your UserForm code is stored.
Paste the following function into the module: Function NormalizeUserFormDate(ByVal rawDate As String) As String Dim cleanDate As String Dim y As Integer, m As Integer, d As Integer cleanDate = Replace(rawDate, "-", "") If Len(cleanDate) <> 8 Then Exit Function y = CInt(Left(cleanDate, 4)) m = CInt(Mid(cleanDate, 5, 2)) d = CInt(Right(cleanDate, 2)) If IsDate(DateSerial(y, m, d)) Then NormalizeUserFormDate = Format(DateSerial(y, m, d), "yyyy-mm-dd") End If End Function
In the UserForm code for the OK or Submit button, use txtDate.Value = NormalizeUserFormDate(txtDate.Value) before saving, and show a MsgBox if the returned value is empty.
Run the UserForm with F5, enter 20240517 and 2024-05-17, and confirm that both values are normalized to 2024-05-17.

Use a calendar-based UserForm for controlled date entry
Use this solution if you prefer to avoid manual date parsing errors and want users to select a date from a calendar control instead of typing it.
Normalize VBA UserForm date input in WPS Writer with a reusable validation macro
WPS Writer includes a VBA-compatible macro editor, so you can paste the same date validation function used in Word, run the UserForm, and store every accepted date as YYYY-MM-DD without changing your workflow.
- 1. Open WPS Writer and enable macros: Open your .docm, .doc, or .dotm file in WPS Writer, enable macros from the security warning or Trust Center, and press Alt+F11 to open the VBA editor.
- 2. Insert a standard module: In the VBA editor Project Explorer, right-click the project, choose Insert > Module, and paste the NormalizeUserFormDate function into the module.
- 3. Connect the function to the UserForm submit button: In the UserForm code, call NormalizeUserFormDate(txtDate.Value) from the OK or Submit button click event and assign the returned value back to the date text box.
- 4. Run and verify both date formats: Press F5 to run the UserForm, enter 20240517 and 2024-05-17, and confirm that the form saves both entries as 2024-05-17.

Frequently Asked Questions
Can I use the same VBA date validation code in WPS Writer and Microsoft Word?
Yes. The validation function uses standard VBA functions such as Replace, DateSerial, IsDate, and Format, so it can be pasted into the VBA editor in WPS Writer or Word. Open the editor with Alt+F11 in either application.
What should the code do if the user enters an invalid date like 20240231?
The DateSerial and IsDate check will fail, so the function returns an empty string. Show a MsgBox to the user, keep focus in the date text box, and do not write the value to the document or database.
Does this function handle dates with slashes or other separators?
The basic version removes hyphens only. If users may enter slashes, add cleanDate = Replace(cleanDate, "/", "") before the length check, then continue with the same eight-character validation.
Is a calendar-based UserForm better than manual text validation?
A calendar control helps prevent typing errors and inconsistent formats, but it may still allow manual input. For mixed input methods, combine the calendar control with the validation function so pasted or typed values are also checked.
Do I need to enable macros in WPS Writer for the UserForm to work?
Yes. Enable macros when the security warning appears or from the Trust Center settings, and save the file in a macro-enabled format such as .docm or .dotm so the UserForm and VBA code remain available.




