VBA - Gradient Algorithm for Conditional Formatting

  • Thread starter Thread starter Legacy 143009
  • Start date Start date
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

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
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,224,800
Messages
6,181,045
Members
453,014
Latest member
Chris258

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