logo
search
VBA & Macro Problems

Fix Word Macro Not Running Second Formatting Operation

WPS EditorWPS Editor Sep 28, 2026 868 views

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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 to open the VBA Editor, insert a new Module, and create a macro using the Find object.

2
Set Up the Wildcard Search

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,}\).

3
Apply Bold Formatting First

Inside the 'Do While .Execute' loop, apply bold formatting to the entire matched selection by adding 'Selection.Font.Bold = True' before applying individual colors.

4
Define Sub-Ranges for Colors

Calculate the character positions for the first number and the subsequent numbers within the matched string to create precise sub-ranges.

5
Apply Specific Colors and Highlights

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 a Single Macro with Wildcard Find and Sequential Formatting
Correct Placement of Bold Formatting: Placing 'Selection.Font.Bold = True' at the beginning of the format loop ensures it applies to the entire matched range without disrupting the specific color assignments that follow.
Advanced Document Formatting

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. 1. Open WPS Writer: Launch WPS Office and open your document in WPS Writer.
  2. 2. Enable Developer Tools: Go to the Developer tab to access the VBA Editor and manage your macros.
  3. 3. Run Wildcard Macros: Paste your formatting macro code and run it seamlessly on your document text.
Fully compatible with Microsoft Word VBA macros and scriptsAdvanced Find and Replace tool with full Wildcard supportLightweight application with high performance for bulk text operationsFree and easy to navigate tabbed user interface
microsoft office alternative - wps office

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.