How to Apply Three-Color Conditional Formatting with VBA in Excel
Question details
The user wants to apply a three-color conditional formatting scale (from green to red) to populated columns starting from column C using a VBA macro.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating data visualization across dynamically populated columns by applying a green-midpoint-red color scale using VBA code.
- Observed behavior
- Needs a reliable VBA script to add the color scale and correctly reference the format conditions to avoid collection-index errors.
Verify that your workbook is saved in a Macro-Enabled format (.xlsm) and ensure the Developer tab is active on your ribbon so you can access the VBA Editor.
Use VBA Macro to Apply a Three-Color Scale
Write a VBA script using the AddColorScale method to apply a green-to-red gradient format and set the correct priority index to prevent errors.
When applying conditional formatting automatically, referencing the active selection incorrectly can cause a run-time error 9. To prevent this, always use `.FormatConditions(.FormatConditions.Count).SetFirstPriority` so the macro accurately points to the most recently added format condition in the collection.
Additionally, ensure you use clear, descriptive variable names in your code. Avoid using native Excel property names (like 'Offset'); use distinct local variables such as 'ColumnOffset' instead.
Press 'Alt + F11' to open the Visual Basic for Applications (VBA) editor. Click 'Insert' > 'Module' to create a new module space for your code.
Create a new subroutine and use the `FormatConditions.AddColorScale ColorScaleType:=3` method to initiate a 3-color scale on your target range.
Set your criteria for the scale: configure the lowest value to display as green, set a 50th-percentile midpoint (often yellow), and set the highest value to red.
Immediately after adding the condition, add the line `.FormatConditions(.FormatConditions.Count).SetFirstPriority` to correctly register the format condition without triggering collection-index errors.
Navigate back to your worksheet. Select cell C1 (or the first cell of your populated columns), press 'Alt + F8', select your macro, and click 'Run'.

Run VBA Macros and Manage Conditional Formatting in WPS Spreadsheet
WPS Spreadsheet offers powerful data visualization tools and comprehensive VBA support. You can seamlessly run your macro scripts, apply advanced conditional formatting, and process data efficiently without compatibility issues.
- 1. Open your Workbook in WPS: Launch WPS Spreadsheet and open your Macro-Enabled Excel file.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon and click 'VBA Editor'.
- 3. Execute the Macro: Paste your 3-color formatting code into a module, select your target starting cell in the sheet, and click 'Run' to apply the color scale.

Frequently Asked Questions
Why do I get 'Run-time error 9' when applying conditional formatting via VBA?
This error occurs when the script tries to reference an index that doesn't match the selection's conditional formatting collection. To fix it, replace `Selection.FormatConditions.Count` with `.FormatConditions(.FormatConditions.Count)`.
Can I apply a three-color scale without using VBA?
Yes. Select the range you want to format, go to the 'Home' tab, click 'Conditional Formatting', hover over 'Color Scales', and select the Green-Yellow-Red scale from the built-in options.
How do I change the specific colors used in the VBA color scale?
You can customize the colors by modifying the `.Color` property of each criterion in the VBA code. Use the `RGB(Red, Green, Blue)` function for `.ColorScaleCriteria(1).FormatColor.Color`, `.ColorScaleCriteria(2)`, and `.ColorScaleCriteria(3)` to assign your exact desired shades.
Why is it bad practice to use 'Offset' as a variable name in VBA?
Using a standard property or method name like 'Offset' as a local variable can confuse the compiler and reduce code clarity, as it clashes with the globally available Excel 'Range.Offset' property. Use a descriptive alternative like 'ColumnOffset'.




