Martin Fishlock
12/15/2006 12:03:00 PM
Keri,
The following changes a chart called 'Chart 1' and with the click of a check
box you can show or hide a series line.
There is also the option for dealing with the legend.
If you have 26 series on the chart then you just have 26 check boxes and run
the code.
If you have radio buttons instead and just want to display one series then
you will have to manage the switch over by using a global variable to remeber
which one is showing.
Option Explicit
'legend must be the same number as the series
Private Sub SwitchSeriesLine(seriesLine As Integer, bShowIt As Boolean,
bHaveLegend)
Dim linestyle As XlLineStyle
Dim linecolor As XlColorIndex
Dim fontcolor As XlColorIndex
Dim fontbckgrd As Long
If bShowIt Then
linestyle = xlContinuous
linecolor = xlAutomatic
fontcolor = xlAutomatic
fontbckgrd = xlBackgroundAutomatic
Else
linestyle = xlNone
linecolor = xlNone
fontcolor = 2
fontbckgrd = xlBackgroundTransparent
End If
With Worksheets(1).ChartObjects("Chart 1").chart
With .SeriesCollection(seriesLine)
.Border.linestyle = linestyle
.MarkerBackgroundColorIndex = linecolor
.MarkerForegroundColorIndex = linecolor
End With
If bHaveLegend Then
With .Legend.LegendEntries(seriesLine).Font
.ColorIndex = fontcolor
.Background = fontbckgrd
End With
End If
End With
End Sub
Private Sub CheckBox1_Click()
SwitchSeriesLine 1, Me.CheckBox1.Value, True
End Sub
Private Sub CheckBox2_Click()
SwitchSeriesLine 2, Me.CheckBox2.Value, True
End Sub
Private Sub CheckBox3_Click()
SwitchSeriesLine 3, Me.CheckBox3.Value, True
End Sub
--
Hope this helps
Martin Fishlock
Please do not forget to rate this reply.
"keri" wrote:
> Hi I am getting frustrated with the code for the chart as below.
> I want the chart series data source to change depending on a checkbox
> linked to a cell. However I seem to have a problem with the line that
> changes the values. (I recorded the original code from a macro). I have
> tried naming the series collection by number but this doesn't work
> either. When I step through the code and get to either of the lines
> that say " ......values = "=R" then I get a run time error 1004
> application or user defined error or a run time error 438 object does
> not support this property or method.
>
>
> Sheets("chart").Select
> ActiveSheet.ChartObjects("Chart 1").Activate
> ActiveChart.ChartArea.Select
>
> If ActiveSheet.Range("a3") = True Then
> ActiveChart.seriescollection("HAMILTON").Select
> ActiveChart.seriescollection("HAMILTON").values = "=R28C4:R34C4"
> Else: ActiveChart.seriescollection("HAMILTON").values =
> "=R2C2:R20C2"
>
> I'd appreciate any advice anyone can give me as to where I am going
> wrong. Thanks.
>
>