Jrules80
Jrules80

Reputation: 178

VBA use gradient colors when a condition fails

How can I set a cell color to show a gradient of colors when a condition fails. I am new to Excel VBA programming and I came across a similar question on here that asked the questioner to use macro recorder to get the code. I have never used a macro recorder and would appreciate if someone could point me in the right direction. Anyhow, I would like the following color gradient for a cell should a condition pass/fail.

This is the color gradient I am looking for:

enter image description here

** UPDATE **

I tried this with Conditional Formatting and my formula looks like this. Seems partly working but I am ending up coloring the cells that I should not be. See below:

enter image description here

Upvotes: 0

Views: 376

Answers (1)

SnowGroomer
SnowGroomer

Reputation: 695

I would suggest formatting though VBA instead of conditional formatting if this is a static color (not changed dynamically as the user is updating Excel).

Here's some generic code on formatting a cell with a gradient from the macro recorder (Selection is the cell we want to fill)

    With Selection.Interior
        .Pattern = xlPatternLinearGradient
        .Gradient.Degree = 90
        .Gradient.ColorStops.Clear
    End With
    With Selection.Interior.Gradient.ColorStops.Add(0)
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    With Selection.Interior.Gradient.ColorStops.Add(1)
        .ThemeColor = xlThemeColorAccent1
        .TintAndShade = 0
    End With

Upvotes: 1

Related Questions