logo
search
VBA & Macro Problems

How to Underline Red Numbers in Blue Using Word VBA

Maira MehtabMaira Mehtab Sep 27, 2026 869 views

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

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the top menu, click 'Insert' and select 'Module' to create a blank workspace for your code.

3
Paste the VBA 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

4
Run the Macro

Close the VBA editor to return to your document. Press Alt + F8, select 'AddBlueUnderlineToRedTextWithNumbers' from the list, and click 'Run' to apply the formatting.

Understanding the Code: The code uses the wildcard pattern '[0-9]{1,}' to find any sequence of digits. It specifies 'wdColorRed' for the search font and 'wdColorBlue' alongside 'wdUnderlineSingle' for the replacement format.
Powerful Document Automation

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. 1. Open your Document in WPS Writer: Launch WPS Office and open the document containing the red numbers.
  2. 2. Access Find and Replace: Press Ctrl + H to open the Find and Replace dialog box.
  3. 3. Configure Wildcard Search: Check 'Use wildcards', type '[0-9]{1,}' into the Find box, and set the Find format color to Red.
  4. 4. Apply Blue Underline: Leave the Replace box empty, set its formatting to a single Blue underline, and click 'Replace All'.
Fully compatible with Microsoft Word document formats (.docx, .doc, .dotm).Advanced Find and Replace features including wildcard and rich formatting search.Built-in VBA editor in WPS Pro/Enterprise versions to run existing Word macros.Lightweight, fast, and free to download for everyday document processing.
QA img-9

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.