How to Underline Red Numbers in Blue Using Word VBA
Question details
The user needs to locate numbers that are formatted in red within a Word document and apply a single blue underline to them automatically using Find and Replace or a VBA macro.
- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating document formatting where specific numeric data colored in red requires an additional blue underline for emphasis or categorization.
- Observed behavior
- Numbers formatted in red font color successfully receive a single blue underline across the selected range or the entire document.
Before running any VBA macro, ensure that your document's Developer tab is enabled and that you have saved a backup copy of your document to prevent accidental formatting changes.
Use a VBA Macro to Underline Red Numbers
The most efficient way to apply a blue underline to red numbers across a document is by running a dedicated VBA macro with wildcard matching.
This macro utilizes Word's Find and Replace functionality with wildcards enabled to precisely target numbers colored red, ensuring no other text or colors are affected.
Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications (VBA) editor.
In the top menu, click 'Insert' and select 'Module' to create a blank workspace for your code.
Copy and paste the following code into the module window: Sub AddBlueUnderlineToRedTextWithNumbers() With Selection.Find .ClearFormatting .Forward = True .Wrap = wdFindStop .Format = True .MatchWildcards = True .Font.Color = wdColorRed .Text = "[0-9]{1,}" With .Replacement .ClearFormatting .Font.Underline = wdUnderlineSingle .Font.UnderlineColor = wdColorBlue End With .Execute Replace:=wdReplaceAll End With End Sub
Close the VBA editor to return to your document. Press Alt + F8, select 'AddBlueUnderlineToRedTextWithNumbers' from the list, and click 'Run' to apply the formatting.
Use Advanced Find and Replace with Wildcards
If you prefer not to use macros, you can manually achieve this formatting using Word's built-in Advanced Find and Replace feature with wildcards.
Format Documents Easily with WPS Writer
WPS Office provides robust support for advanced document formatting, including wildcard Find & Replace and comprehensive VBA macro capabilities (in supported versions) to automate repetitive tasks.
- 1. Open your Document in WPS Writer: Launch WPS Office and open the document containing the red numbers.
- 2. Access Find and Replace: Press Ctrl + H to open the Find and Replace dialog box.
- 3. Configure Wildcard Search: Check 'Use wildcards', type '[0-9]{1,}' into the Find box, and set the Find format color to Red.
- 4. Apply Blue Underline: Leave the Replace box empty, set its formatting to a single Blue underline, and click 'Replace All'.

Frequently Asked Questions
Can I change the macro to underline different colors?
Yes, you can modify the VBA code by changing 'wdColorRed' and 'wdColorBlue' to other built-in Word color constants like 'wdColorGreen', or by using specific RGB values such as RGB(255, 0, 0) for red and RGB(0, 0, 255) for blue.
What does the wildcard [0-9]{1,} do in the search?
The expression [0-9]{1,} instructs the Find feature to search for any continuous sequence of digits from 0 to 9, ensuring that both single isolated numbers and multi-digit numbers are matched perfectly.
Why did the macro underline text that isn't red?
This usually happens if the search criteria weren't cleared from a previous operation. The macro uses '.ClearFormatting' before setting '.Font.Color = wdColorRed' to prevent this. Ensure you copy the macro exactly as provided so the color restriction applies properly.
Will this macro work on only a selected portion of text?
As written, the macro uses 'wdReplaceAll', which typically processes the entire document. If you only want to affect a specific highlighted area, you can change 'wdReplaceAll' to 'wdReplaceAll' but ensure your search scope (.Wrap = wdFindStop) is configured to not prompt to search the rest of the document.




