How to Shade Table Cells Based on Values Using Word VBA Macros
Question details
The user needs a Word VBA macro to conditionally shade table cells based on specific text values, specifically turning cells containing 'Yes' yellow and 'No' red.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Applying conditional formatting to a Word table to visually highlight specific data points based on the text they contain.
- Observed behavior
- Word lacks built-in conditional formatting for native tables, requiring a VBA macro to read cell contents and apply background colors programmatically.
Running macros can permanently alter document formatting in ways that cannot be undone via the standard Undo button, so save a backup copy of your Word document before proceeding.
Create and Run a VBA Macro to Conditionally Shade Cells
Use a custom VBA macro to iterate through table cells, evaluate the text, and apply specific background colors based on your conditions.
Since Word does not have native conditional formatting for tables like Excel does, VBA (Visual Basic for Applications) is required to automate the process. The macro must loop through each cell, clean the text string, and apply the Shading.BackgroundPatternColor.
Press ALT + F11 on your keyboard to launch the Microsoft Visual Basic for Applications window.
In the top menu, click Insert and select Module to create a blank workspace for your macro code.
Write a For Each loop to iterate through the table's cells. Inside the loop, extract the cell text and remove the end-of-cell marker (ASCII characters 13 and 7) using the Left() function, as Word adds these hidden characters to every cell.
Add an If or Select Case statement. For the 'Yes' condition, set Cell.Shading.BackgroundPatternColor = wdColorYellow. For the 'No' condition, set it to wdColorRed.
Close the VBA Editor. In your Word document, click inside the target table, press ALT + F8 to open the Macros dialog, select your newly created macro, and click Run.

Use an Embedded Excel Spreadsheet for Native Conditional Formatting
If your table requires advanced or frequently updated conditional formatting, embedding an Excel spreadsheet is much easier than maintaining Word macros.
Handle Advanced Tables and Macros Seamlessly in WPS Office
WPS Office provides robust support for document formatting, advanced tables, and VBA macros (available in the professional version), allowing you to automate repetitive tasks and seamlessly embed spreadsheets without slowing down your workflow.
- 1. Enable the Developer Tab: Open your document in WPS Writer and navigate to the Developer tab to access the macro tools.
- 2. Launch the VBA Editor: Click on the Macros button or Visual Basic editor icon to input your VBA code for cell shading.
- 3. Use Embedded Excel Alternately: If you prefer not to code, go to the Insert tab, click Table, and choose Excel Spreadsheet to use standard conditional formatting.

Frequently Asked Questions
Why does my VBA macro fail to recognize the text inside the Word table cell?
Word table cells include a hidden end-of-cell marker consisting of a carriage return and a bell character. If your macro does not use a function to strip these characters from the string before comparing it to 'Yes' or 'No', the conditional check will return false and the shading will not be applied.
Can I use Excel's conditional formatting directly on native Word tables?
No, Word does not feature native conditional formatting for its standard tables. To utilize standard conditional formatting rules without writing VBA code, you must insert an embedded Excel spreadsheet via the Insert > Table menu.
How do I change the macro to highlight a different word or phrase?
Open the VBA Editor by pressing ALT + F11, locate the If or Select Case statement in your macro code, and replace 'Yes' or 'No' with your new target word. Ensure that the text casing matches perfectly, or use the UCase() function in your code to make the comparison case-insensitive.




