logo
search
VBA & Macro Problems

How to Use VBA to Replace X with 0 in Five-Digit Values at Word Paragraph Starts

Maira MehtabMaira Mehtab Sep 25, 2026 870 views

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.

How to Use VBA to Replace X with 0 in Five-Digit Values at the Start of Word Paragraphs
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 you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 to open the VBA Editor in Word.

2
Insert a New Module

Click Insert > Module in the top menu to create a new module.

3
Set Up the Find Method

Write a VBA macro that uses the .Find method with .MatchWildcards = True and the search pattern ^[0-9X]{5} to locate the target strings.

4
Apply the Replacement Logic

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'.

5
Run the Macro

Press F5 or click the Run button to execute the macro and process the entire document.

Use a VBA Macro with Wildcard Search
Wildcard Syntax: The caret symbol (^) in a wildcard search represents the beginning of a paragraph, while [0-9X]{5} specifies exactly five characters consisting of digits or the uppercase letter X.

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. 1. Open Your Document: Launch WPS Writer and open your target document.
  2. 2. Access Find and Replace: Press Ctrl + H to access the Find and Replace dialog.
  3. 3. Enable Advanced Search: Expand the advanced options menu to enable 'Use wildcards'.
  4. 4. Search and Edit: Enter your specific search pattern to accurately locate string combinations.
Full compatibility with Microsoft Word documents (.docx).Advanced Find & Replace tool with comprehensive wildcard support.Powerful Macro capabilities to automate repetitive editing tasks.Free and lightweight office suite for Windows, Mac, and Linux.
microsoft office alternative - wps office

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.