How to Fix Word VBA Formatting After Removing Prefix Characters
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 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.
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.
Press ALT + F11 to open the Visual Basic for Applications (VBA) editor and locate the module containing your specific formatting macro.
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.
Set the '.Replacement.Text' property to '\1'. This backreference ensures that the captured text group is kept while the prefix characters are discarded.
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'.
Run '.Execute Replace:=wdReplaceAll' to perform the find, replace, and formatting operations seamlessly across the designated range or document.

Use Word Styles Instead of VBA Macros
Create a document template with predefined styles for a faster and more reliable formatting approach without requiring VBA code.
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. Open Your Document in WPS Writer: Launch WPS Office and open your .docx file containing the text that requires formatting.
- 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. 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. 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'.

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.




