Excel-VBA gradient colorstops

后端 未结 1 1360
温柔的废话
温柔的废话 2021-01-25 09:43

What is the best way to apply horizontal gradient fill effect to a cell through macro code?

I\'ve set the desired gradient in Excel (right-click on cell B1, Format Cells

1条回答
  •  悲哀的现实
    2021-01-25 10:00

    The assignment of ColorStops is not valid code. You need to add the colorstops and then set their properties. The macro recorder does it correctly.

    Sub SetGradient()
    Dim myrange As Range
    Set myrange = ThisWorkbook.Sheets("Sheet1").Range("B1")
    
        With myrange.Interior
            .Pattern = xlPatternLinearGradient
            .Gradient.Degree = 90
            .Gradient.ColorStops.Clear
        End With
        With myrange.Interior.Gradient.ColorStops.Add(0)
            .Color = 16777215
            .TintAndShade = 0
        End With
        With myrange.Interior.Gradient.ColorStops.Add(0.5)
            .Color = 7961087
            .TintAndShade = 0
        End With
        With myrange.Interior.Gradient.ColorStops.Add(1)
            .Color = 16777215
            .TintAndShade = 0
        End With
    End Sub
    

    0 讨论(0)
提交回复
热议问题