logo
search
Formatting Issues

How to Add Yellow Highlighting Using Word VBA Find and Replace

Muhammad TalhaMuhammad Talha Sep 28, 2026 869 views

Question details

The user wants to modify a VBA macro to apply yellow highlighting to specific names using Find and Replace without altering their current formatting.

How to Add Yellow Highlighting with Word VBA
Product
Microsoft Word
Device & OS
not provided
Scenario
Writing or editing a VBA macro to automatically find an array of names in a document and emphasize them with highlight formatting.
Observed behavior
The goal is to apply a highlight color dynamically via VBA while retaining the text's existing bold weight and font colors.
Before you start

Ensure you have the Developer tab enabled in Word to access the VBA editor, and verify that your default ribbon highlight color is set to yellow before running the macro.

Solution 1Recommended

Use the Replacement.Highlight Property in VBA

Modify your Find and Replace macro loop to enable the highlight property for the replacement text object.

By setting the Replacement.Highlight property to True, Microsoft Word applies the current system highlight color (which defaults to yellow) to the matched text. Because you are only modifying the highlight attribute, all other existing text styles, such as bold text or specific font colors, remain completely intact during the replacement process.

1
Open the Visual Basic Editor

Press Alt + F11 on your keyboard to launch the Microsoft Visual Basic for Applications window.

2
Locate Your Macro Code

In the Project Explorer, double-click the module that contains your Find and Replace array macro.

3
Add the Highlight Command

Inside your With Selection.Find block, insert the code line '.Replacement.Highlight = True'.

4
Execute the Replacement

Ensure your code triggers the replacement by using 'Selection.Find.Execute Replace:=wdReplaceAll'. Run the macro to highlight all names in your array.

Use the Replacement.Highlight Property in VBA
Highlight Color Default: The .Replacement.Highlight property applies whatever color is currently selected in your Word toolbar. To force it to yellow programmatically, you can add 'Options.DefaultHighlightColorIndex = wdYellow' before your Find loop.

Easily Highlight Text and Run Macros with WPS Writer

WPS Writer offers an intuitive Advanced Find and Replace tool to apply highlights without coding, alongside robust macro support for automated formatting tasks.

  1. 1. Open Find and Replace: Launch WPS Writer and press Ctrl + H to open the Find and Replace dialog box.
  2. 2. Input the Target Text: Type the specific name or word you wish to highlight into the 'Find what' field.
  3. 3. Apply Highlight Formatting: Click the 'Format' button, select 'Highlight', and ensure the color is set to yellow.
  4. 4. Replace All: Click 'Replace All' to instantly apply the yellow highlight to all matching instances while preserving original fonts.
Seamless format compatibility with Microsoft Word (.docx)Built-in formatting tools in Find and Replace to bypass VBASupports powerful automation with macrosLightweight software with a familiar, easy-to-navigate interface
microsoft office alternative - wps office

Frequently Asked Questions

Can I specify a different highlight color directly in the VBA macro?

Yes, you can change the default highlight color before running the replacement by adding the line Options.DefaultHighlightColorIndex = wdGreen (or your preferred color constant) at the beginning of your macro.

Why does my macro remove the bold formatting when replacing the text?

This happens if your macro includes the line .Replacement.ClearFormatting. Remove or comment out this line to ensure the macro preserves existing text formats like bolding and font color.

How do I highlight an array of multiple names at once?

You can use a 'For Each' loop to iterate through an array of strings in your VBA code. Inside the loop, assign the current array item to .Find.Text, set .Replacement.Highlight = True, and execute the wdReplaceAll command.

Will these VBA macros work if I share the document with others?

Yes, provided you save the document as a Macro-Enabled Word Document (.docm). Users who open the file will need to enable macros in their security settings to run your Find and Replace script.