Microsoft Office Tutorials and References
In Depth Information
Applying a random color gradient
.ColorIndex = 3
.Pattern = xlSolid
End With
ActiveChart.SeriesCollection(2).Select
With Selection.Interior
.ColorIndex = 5
.Pattern = xlSolid
End With
ActiveChart.SeriesCollection(3).Select
With Selection.Interior
.ColorIndex = 4
.Pattern = xlSolid
End With
End Sub
Applying a random color gradient
The goal of this exercise is to create a macro that applies a random two-color
gradient to a chart series. If you turn on the macro recorder while you apply a two-color
gradient fill to a series in a column chart, you’ll generate code something like this:
Sub Macro1()
ActiveSheet.ChartObjects(“Chart 1”).Activate
ActiveChart.SeriesCollection(1).Select
Selection.Fill.TwoColorGradient _
Style:=msoGradientVertical, Variant:=3
With Selection
.Fill.Visible = True
.Fill.ForeColor.SchemeColor = 10
.Fill.BackColor.SchemeColor = 8
End With
End Sub
Although this macro contains some irrelevant code, it does reveal some
interesting things. In particular, there is a TwoColorGradient method, and it takes two
arguments ( Style and Variant ). Also the SchemeColor property is used to specify
the two colors. I consulted the VBA Help system to find out more about these
objects and properties.
The macro that follows generates random numbers within the appropriate range
for the TwoColorGradient arguments and the SchemeColor property. It’s a bit
complicated because the Style argument of 6 is invalid. In addition, the Variant
argument can be either 1 or 2 when the Style argument is 7. The macro uses If
statements to handle these exceptions.
Search JabSto ::




Custom Search