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