I am not sure where this error comes from, but it is thrown even if I comment out everything after the data assignment.
I am new to VBA and have tried many variations of this code and spent hours on it in the past few days, but haven't been able to avoid this problem.
Sub fullPageLine()
Dim rng As Range
Dim cht As Object
'Data range for the chart
Set rng = Selection
'Create a chart
Set cht = ActiveSheet.Shapes.AddChart2(227, xlLine).Select
'Give chart some data
cht.Chart.SetSourceData Source:=rng
cht.Activate
'Reposition Title
With ActiveChart.ChartTitle
.Left = 24.632
.Top = 6
End With
'Format x axis
ActiveChart.Axes(xlCategory).Select
With Selection.Format.TextFrame2.TextRange.Font
.NameComplexScript = "Arial"
.NameFarEast = "Arial"
.Name = "Arial"
End With
Selection.Format.TextFrame2.TextRange.Font.Size = 7
'Format y axis
ActiveSheet.ChartObjects("cht").Activate
ActiveChart.Axes(xlValue).Select
With Selection.Format.TextFrame2.TextRange.Font
.NameComplexScript = "Arial"
.NameFarEast = "Arial"
.Name = "Arial"
End With
Selection.Format.TextFrame2.TextRange.Font.Size = 7
'Format title
ActiveSheet.ChartObjects("cht").Activate
ActiveChart.ChartTitle.Select
With Selection.Format.TextFrame2.TextRange.Font
.NameComplexScript = "Arial"
.NameFarEast = "Arial"
.Name = "Arial"
End With
Selection.Format.TextFrame2.TextRange.Font.Size = 8.4
Selection.Left = 23.632
Selection.Top = 6
'Format legend
ActiveSheet.ChartObjects("cht").Activate
ActiveChart.Legend.Select
With Selection.Format.TextFrame2.TextRange.Font
.NameComplexScript = "Arial"
.NameFarEast = "Arial"
.Name = "Arial"
End With
Selection.Format.TextFrame2.TextRange.Font.Size = 7
'Change chart series fill color
ActiveSheet.ChartObjects("cht").Activate
With ActiveChart.FullSeriesCollection(2).Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent2
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
End With
ActiveSheet.ChartObjects("cht").Activate
With ActiveChart.FullSeriesCollection(3).Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
End With
ActiveSheet.ChartObjects("cht").Activate
With ActiveChart.FullSeriesCollection(4).Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.5
.Transparency = 0
End With
ActiveSheet.ChartObjects("cht").Activate
With ActiveChart.FullSeriesCollection(5).Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.349999994
.Transparency = 0
End With
End Sub
I am looking to produce a chart with my specified color and formatting preferences, but this macro as is only produces the default formatted excel chart from the data I select.