logo
search
VBA & Macro Problems

How to Fix Word VBA Formatting After Removing Prefix Characters

WPS EditorWPS Editor Oct 1, 2026 869 views

Question details

The user is attempting to run a Word VBA macro to remove prefix characters (such as three hash signs) and format the remaining text, but the formatting is not being applied correctly.

Product
Word
Device & OS
not provided
Scenario
Using a VBA wildcard find and replace operation to clean up document formatting, such as standardizing a résumé template.
Observed behavior
The macro successfully deletes the prefix hash signs, but fails to apply the intended font formatting (bold, underline, size) because Selection.Range targets the wrong text area after the replacement.
Before you start

Before running or modifying VBA macros, ensure you have saved a backup copy of your document. If you are formatting a highly structured document like a résumé, evaluate if built-in Word Styles might be a simpler, more stable alternative to automated VBA formatting.

Solution 1Recommended

Apply Formatting Directly to the Replacement Range in VBA

Modify your VBA code to explicitly target the replacement object's font properties rather than relying on the general selection range.

When using wildcards in VBA to strip prefix characters (for example, searching for '###(*)' and replacing it with '\1'), the active selection range does not automatically snap to the newly inserted text. Applying formatting to Selection.Range will often format the wrong text. To resolve this, you must apply your formatting commands directly to the Replacement.Font property within your Find/Replace execution.

1
Open the VBA Editor

Press ALT + F11 to open the Visual Basic for Applications (VBA) editor and locate the module containing your specific formatting macro.

2
Set Up the Wildcard Search

Ensure your Find object is configured properly. Set '.MatchWildcards = True'. Use the search string '###(*)' in the '.Text' property to locate the hash signs and capture all subsequent text in the group.

3
Define the Replacement Text

Set the '.Replacement.Text' property to '\1'. This backreference ensures that the captured text group is kept while the prefix characters are discarded.

4
Assign Formatting to the Replacement Object

Apply formatting directly to the replacement font object. Add lines such as '.Replacement.Font.Bold = True', '.Replacement.Font.Underline = wdUnderlineSingle', and '.Replacement.Font.Size = 12'.

5
Execute the Replace Operation

Run '.Execute Replace:=wdReplaceAll' to perform the find, replace, and formatting operations seamlessly across the designated range or document.

Apply Formatting Directly to the Replacement Range in VBA
Code Validation: Applying formatting directly to the Replacement object ensures that only the text matching the '\1' wildcard group receives the new font styles.
Advanced Document Formatting

Format and Edit Macros Easily with WPS Office

WPS Writer offers comprehensive support for VBA macros, advanced wildcard search operations, and intuitive document formatting. It allows you to efficiently clean up text and format complex documents like résumés with complete MS Word compatibility.

  1. 1. Open Your Document in WPS Writer: Launch WPS Office and open your .docx file containing the text that requires formatting.
  2. 2. Open the Find and Replace Tool: Press Ctrl + H to open the Replace dialog. Click on 'Options' or 'More' to reveal advanced search settings, then check the 'Use wildcards' box.
  3. 3. Enter Wildcard Search Parameters: Type '###(*)' in the 'Find what' field and type '\1' in the 'Replace with' field to instruct WPS to remove the hash signs.
  4. 4. Apply Direct Formatting: Click inside the 'Replace with' text box, then click 'Format' > 'Font' at the bottom of the dialog. Apply your desired bold, underline, and size settings, then click 'Replace All'.
Full support for executing and editing VBA macrosAdvanced wildcard Find and Replace capabilities without needing to codeSeamless format compatibility with Microsoft Word (.docx)Intuitive built-in Style manager for reliable document formatting
microsoft office alternative - wps office

Frequently Asked Questions

Why does Selection.Range fail to format text after a wildcard replacement?

When a VBA macro executes a find and replace operation, the active selection range typically remains at the start of the search or encompasses the original search boundaries. It does not automatically adjust to target the newly inserted replacement text, causing formatting commands on Selection.Range to apply incorrectly.

What does the \1 mean in a wildcard replacement?

The '\1' is a backreference used in regular expressions and wildcard searches. It represents the first grouped expression in your search string (the part enclosed in parentheses). Using '\1' in the replacement field tells the application to keep that captured text while discarding anything outside the parentheses.

Can I use wildcard replace to remove characters without VBA?

Yes, you can use the built-in Find and Replace dialog (Ctrl+H) in your word processor. Simply check the 'Use wildcards' option, enter your search and replace parameters, and even specify formatting for the replacement text directly through the dialog box interface without writing any code.

Are Word Styles better than VBA for document formatting?

For structured documents like résumés or reports, predefined styles are generally more dependable and easier to maintain than applying direct formatting via VBA macros. Styles ensure formatting consistency across the entire document and can be updated globally with a single modification.