VBA & Macro Problems

Excel VBA Macro to Shade Rows Based on Cell Values

Elise WilliamsElise Williams Nov 3, 2026 868 views

Question details

The user wants an Excel VBA macro to check cells in specific columns and highlight the entire row red if any cell does not match an exact required statement.

How to Create an Excel VBA Macro to Shade Rows Based on Cell Values
Product
Microsoft Excel
Device & OS
not provided
Scenario
Reviewing imported daily survey results to visually flag rows that contain unexpected or incorrect responses.
Observed behavior
The user needs a working macro script, as previous attempts resulted in syntax errors from stray characters and incorrect color codes.
Before you start

Ensure your workbook is saved as an Excel Macro-Enabled Workbook (.xlsm) and that the Developer tab is enabled in your ribbon to access the VBA Editor.

Solution 1Recommended

Write and Run a VBA Macro to Highlight Rows Red

Create a VBA loop that evaluates the specific range of columns in each row and changes the background color to red if the required string is not found.

To accomplish this task, the VBA script needs to iterate through each row in your dataset, specifically checking columns L through V. If it detects a mismatch against your exact required statement (e.g., 'THIS STATEMENT IS TRUE AND ACCURATE'), it applies ColorIndex 3 (red) to the entire row.

It is crucial to remove any stray asterisks or formatting artifacts from sample code before running it, as these will trigger syntax errors.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor. Alternatively, navigate to the Developer tab and click on 'Visual Basic'.

2
Insert a New Module

In the VBA Editor, click on 'Insert' in the top menu and select 'Module'. This will open a blank window where you can write your macro script.

3
Enter the VBA Code

Paste your custom script into the module. Ensure the code uses a loop to check cells in columns L:V against your exact statement. In the portion of the code that highlights the row, set the property to '.Interior.ColorIndex = 3' to achieve the red shading.

4
Assign the Macro to a Button

Go back to your worksheet, click the Insert tab, and add a Button or Shape. Right-click the newly inserted object, select 'Assign Macro', choose your row-shading macro, and click OK. You can now run the macro by simply clicking this button.

Write and Run a VBA Macro to Highlight Rows Red
ColorIndex Reference: In Excel VBA, ColorIndex 3 represents standard red, while ColorIndex 15 represents grey. Always double-check this index if you decide to change your highlight colors later.
WPS Spreadsheet Automation

Run VBA Macros Seamlessly in WPS Spreadsheet

WPS Spreadsheet offers powerful support for VBA macros, enabling you to automate repetitive tasks like formatting and shading rows. Its intuitive interface provides a familiar environment for running your custom scripts without complex configurations.

  1. 1. Enable the Developer Tools: Open WPS Spreadsheet, go to the Options menu, and ensure the Developer tab is enabled to access macro features.
  2. 2. Open the VBA Editor: Click on the Developer tab in the ribbon and select 'Visual Basic', or use the Alt + F11 shortcut to open the code editor.
  3. 3. Insert and Run Your Script: Insert a new module, paste your row-shading macro code, and execute it directly from the editor or assign it to a worksheet button.
Highly compatible with Microsoft Excel .xlsm files and VBA scriptsFamiliar Developer tab and Visual Basic Editor interfaceLightweight architecture ensures macros run quickly without freezingCost-effective alternative for advanced spreadsheet automation
microsoft office alternative - wps office

Frequently Asked Questions

Why is my VBA macro producing a syntax error?

Syntax errors often occur if invalid characters, such as stray asterisks or unclosed quotation marks, are accidentally copied into the script. Review the specific line highlighted in yellow in the VBA editor to locate and remove these errors.

How do I change the highlight color in my macro?

You can change the color by modifying the ColorIndex property in your VBA code. Setting 'ColorIndex = 3' makes the row red, 'ColorIndex = 6' makes it yellow, and 'ColorIndex = 4' makes it green.

Why does my macro disappear when I close the workbook?

If you save your file as a standard Excel Workbook (.xlsx), all VBA macros will be permanently stripped out. Always save files containing macros as an Excel Macro-Enabled Workbook (.xlsm).

Can I check for partial text matches instead of an exact statement?

Yes, you can modify the VBA code to use the 'InStr' function, which allows the script to search for a specific substring within the cell value rather than requiring an exact, case-sensitive match.