Hi all,
I recorded a macro to create a two y-axis chart. When I tried to assign the code to a command button and to create the chart I'm getting:
Run-time error '1004'
Method 'Axis' of object '_Chart' failed
The last few lines cause the error. Here's the code:
Sub CreateChart()
Charts.Add
ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:= _
"Line - Column on 2 Axes"
ActiveChart.SetSourceData Source:=Sheets("SpeedTable" ).Range("G19" ), PlotBy _
:=xlColumns
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).XValues = "=SpeedTable!R3C1:R508C1"
ActiveChart.SeriesCollection(1).Values = "=SpeedTable!R3C2:R508C2"
ActiveChart.SeriesCollection(1).name = "=""Speed"""
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Values = "=SpeedTable!R3C4:R508C4"
ActiveChart.SeriesCollection(2).name = "=""Gear"""
ActiveChart.Location Where:=xlLocationAsObject, name:="Graph"
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Speed/Shift Chart"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time [sec]"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Speed [kph]"
.Axes(xlCategory, xlSecondary).HasTitle = False 'causes the error
.Axes(xlValue, xlSecondary).HasTitle = True 'causes the error
.Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = "Gear" 'causes the error
End With
End Sub
Anyone have any suggestions?
Thanks,
Jason
I recorded a macro to create a two y-axis chart. When I tried to assign the code to a command button and to create the chart I'm getting:
Run-time error '1004'
Method 'Axis' of object '_Chart' failed
The last few lines cause the error. Here's the code:
Sub CreateChart()
Charts.Add
ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:= _
"Line - Column on 2 Axes"
ActiveChart.SetSourceData Source:=Sheets("SpeedTable" ).Range("G19" ), PlotBy _
:=xlColumns
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).XValues = "=SpeedTable!R3C1:R508C1"
ActiveChart.SeriesCollection(1).Values = "=SpeedTable!R3C2:R508C2"
ActiveChart.SeriesCollection(1).name = "=""Speed"""
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Values = "=SpeedTable!R3C4:R508C4"
ActiveChart.SeriesCollection(2).name = "=""Gear"""
ActiveChart.Location Where:=xlLocationAsObject, name:="Graph"
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Speed/Shift Chart"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time [sec]"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Speed [kph]"
.Axes(xlCategory, xlSecondary).HasTitle = False 'causes the error
.Axes(xlValue, xlSecondary).HasTitle = True 'causes the error
.Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = "Gear" 'causes the error
End With
End Sub
Anyone have any suggestions?
Thanks,
Jason