VBA - Gradient Algorithm for Conditional Formatting

L

Legacy 143009

Guest
Hi,

I decided to use VBA regarding the post below:

Does anyone know the 3-color gradient algorithm that excel uses from green-to yellow-to red for conditional formatting?
I am going to have decimal values from 0.0 to 1.0. How can adjust RGB values accordingly in such a way that cells' interior colors will vary from red to green?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I came up with this solution. It works for me. I hope someone will benefit it, too..

Cells(i, 1).Interior.Color = RGB(Round(255 * (1 - x), 0), Round(255 * x, 0), 0)
 
Upvote 0
A more pleasing version:
VBA Code:
Dim x As Double
Dim r As Integer, g As Integer

 x = Cells(i, 1).Value
  If x > 0.5 Then
    r = 255 * (2 * (1 - x))
  Else
    r = 255
  End If
  If x < 0.5 Then
     g = 255 * (2 * x)
   Else
     g = 255
 End If
 Cells(i, 1).Interior.Color = RGB(r, g, 0)
 
Upvote 0
Solution

Forum statistics

Threads
1,215,453
Messages
6,124,930
Members
449,195
Latest member
Stevenciu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top