How to Use VBA to Replace X with 0 in Five-Digit Values at Word Paragraph Starts
Question details
The user needs a Word VBA macro to automatically find five-character strings at the beginning of paragraphs and replace the character 'X' with '0' within those specific strings.

- Product
- Microsoft Word / WPS Writer
- Device & OS
- not provided
- Scenario
- Automating text replacement within specific five-digit patterns located exclusively at the beginning of document paragraphs.
- Observed behavior
- Currently, the user is manually deleting or replacing 'X' with '0', which is time-consuming and inefficient for large documents.
Before running any VBA macro, ensure you save a backup copy of your document to prevent accidental data loss during the find-and-replace operation.
Use a VBA Macro with Wildcard Search
Run a VBA script that iterates through the document, finds 5-character patterns starting paragraphs, and replaces 'X' with '0'.
This method uses Word's wildcard search functionality within a VBA loop. It targets specific patterns (combinations of digits and the letter X) only if they appear at the very beginning of a paragraph, ensuring other parts of your text remain unaffected.
Press ALT + F11 to open the VBA Editor in Word.
Click Insert > Module in the top menu to create a new module.
Write a VBA macro that uses the .Find method with .MatchWildcards = True and the search pattern ^[0-9X]{5} to locate the target strings.
Inside your macro's search loop, use the Replace function on the .Text property of the found Range object to change the character 'X' to '0'.
Press F5 or click the Run button to execute the macro and process the entire document.

Perform a Manual Wildcard Find and Replace
If you prefer not to use VBA, you can step through the document using the built-in advanced Find feature.
Automate Document Editing with WPS Office
WPS Writer provides robust support for macros and advanced Find & Replace features, including wildcards. You can easily manage complex document formatting and data cleanup tasks.
- 1. Open Your Document: Launch WPS Writer and open your target document.
- 2. Access Find and Replace: Press Ctrl + H to access the Find and Replace dialog.
- 3. Enable Advanced Search: Expand the advanced options menu to enable 'Use wildcards'.
- 4. Search and Edit: Enter your specific search pattern to accurately locate string combinations.

Frequently Asked Questions
Why is my VBA macro replacing 'X' everywhere instead of just at the start of paragraphs?
If your macro replaces 'X' globally, the .Find method is likely missing the paragraph-start wildcard (^) or your replacement logic is being applied to the entire document's text rather than the specific Range object that was found.
Can I use this wildcard search to find strings of different lengths?
Yes, you can modify the number inside the curly braces. For example, {3} will look for exactly three characters, and {3,5} will search for strings containing between three and five characters.
Do WPS Office macros support VBA?
WPS Office supports VBA in its premium or specialized enterprise versions, allowing you to run scripts similar to Microsoft Word. Additionally, free WPS Office versions support JavaScript Macros (JSA) as a modern alternative for automation.




