Fix Word Macro Not Running Second Formatting Operation
Question details
The user needs a Word macro to apply multiple distinct formats (font color, highlighting, and bold) to specific parts of a matched text string, such as All(58-5-11-12).
- Product
- Word
- Device & OS
- not provided
- Scenario
- Running a VBA macro to format a specific pattern of numbers inside parentheses with different colors and highlights in a single pass.
- Observed behavior
- The macro successfully applies the first formatting operation but fails to execute the second formatting operation on the remaining numbers.
Ensure you have enabled the Developer tab in your word processor to access the VBA editor, and always back up your document before running wildcard macros that alter text formatting.
Use a Single Macro with Wildcard Find and Sequential Formatting
Combine the operations into a single wildcard search and systematically apply formatting to distinct character ranges within the match.
When VBA macros fail to run subsequent formatting operations on the same string, it is usually because the selection range is lost, collapsed, or modified after the first operation. Using wildcards combined with specific range targeting resolves this issue by keeping the parent match intact while modifying its subsections.
Press Alt + F11 to open the VBA Editor, insert a new Module, and create a macro using the Find object.
Enable wildcard searching by setting '.MatchWildcards = True' and configure the '.Text' property to match the target pattern, for example: All\([0-9]{1,}-[0-9]{1,}-[0-9]{1,}-[0-9]{1,}\).
Inside the 'Do While .Execute' loop, apply bold formatting to the entire matched selection by adding 'Selection.Font.Bold = True' before applying individual colors.
Calculate the character positions for the first number and the subsequent numbers within the matched string to create precise sub-ranges.
Set the first number's range font color to red and highlight color to yellow (wdYellow). Then, set the range for the remaining numbers to a turquoise highlight (wdTurquoise).

Use WPS Writer to Automate Complex Document Formatting
WPS Writer offers robust support for VBA macros and wildcard searches, allowing you to easily automate multi-step text formatting and data styling with excellent Microsoft Office compatibility.
- 1. Open WPS Writer: Launch WPS Office and open your document in WPS Writer.
- 2. Enable Developer Tools: Go to the Developer tab to access the VBA Editor and manage your macros.
- 3. Run Wildcard Macros: Paste your formatting macro code and run it seamlessly on your document text.

Frequently Asked Questions
Why does my macro stop formatting after the first Find and Replace?
This typically happens if the active selection range is altered, collapsed, or loses focus during the first operation. The macro then fails to point to the correct text for the second formatting operation.
How do I enable wildcards in a Word VBA macro?
Within your macro's Find block, set the property '.MatchWildcards = True'. This allows you to use regular-expression-like patterns such as brackets '[0-9]' to find specific number combinations and lengths.
Can I apply two different highlight colors in a single Find operation?
Yes. You can achieve this by capturing the entire matched string as a Range object, calculating the start and end positions of the specific substrings, and applying distinct highlight colors (like wdYellow and wdTurquoise) to those exact sub-ranges.




