VBA & Macro Problems

How to Apply Three-Color Conditional Formatting with VBA in Excel

Rana GarciaRana Garcia Nov 3, 2026 868 views

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.

How to Apply Three-Color Conditional Formatting with VBA in Excel
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press 'Alt + F11' to open the Visual Basic for Applications (VBA) editor. Click 'Insert' > 'Module' to create a new module space for your code.

2
Write the Format Condition Code

Create a new subroutine and use the `FormatConditions.AddColorScale ColorScaleType:=3` method to initiate a 3-color scale on your target range.

3
Configure the Color Scale Criteria

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.

4
Set the First Priority

Immediately after adding the condition, add the line `.FormatConditions(.FormatConditions.Count).SetFirstPriority` to correctly register the format condition without triggering collection-index errors.

5
Select the Starting Cell and Run

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'.

Use VBA Macro to Apply a Three-Color Scale
Avoid Run-Time Error 9: Using `.FormatConditions(.FormatConditions.Count)` ensures your script correctly matches the index to the newly created conditional-formatting collection for the current range.
Advanced Automation Supported

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. 1. Open your Workbook in WPS: Launch WPS Spreadsheet and open your Macro-Enabled Excel file.
  2. 2. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon and click 'VBA Editor'.
  3. 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.
Fully compatible with Microsoft Excel (.xlsx and .xlsm) filesBuilt-in VBA editor to run, edit, and debug macros effortlesslyIntuitive UI for applying color scales manually without codingLightweight, fast, and free to download
microsoft office alternative - wps office

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'.