VBA & Macro Problems

Validate YYYYMMDD and YYYY-MM-DD Dates in WPS Writer VBA UserForms

John WilsonJohn Wilson Nov 13, 2026 869 views

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.

Validate YYYYMMDD and YYYY-MM-DD Dates in WPS Writer VBA UserForms
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

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.

2
Add the validation function

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

3
Call the function from the submit button

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.

4
Test both accepted date formats

Run the UserForm with F5, enter 20240517 and 2024-05-17, and confirm that both values are normalized to 2024-05-17.

Validate and normalize YYYYMMDD or YYYY-MM-DD input with VBA code
Valid dates are normalized: When the function returns a non-empty string, the date is valid and formatted as YYYY-MM-DD. If it returns an empty string, keep focus in the date field and ask the user to enter a valid date.

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. 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. 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. 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. 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.
Accepts both YYYYMMDD and YYYY-MM-DD input and normalizes them to one consistent date format.Maintains .docm, .dotm, and Word VBA macro compatibility when opening or saving files in WPS Writer.Uses a familiar Word-style VBA editor and UserForm controls for a smooth transition.Free, lightweight Office alternative for desktop VBA tasks without a Microsoft 365 subscription.
microsoft office alternative - wps office

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.