我不确定此错误来自何处,但是即使我在数据分配后注释掉所有内容,也会抛出该错误。
我是VBA的新手,在过去几天中尝试了此代码的许多变体并花了数小时,但仍无法避免此问题。
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
我希望生成具有指定颜色和格式设置首选项的图表,但此宏仅会根据我选择的数据生成默认格式的excel图表。
答案 0 :(得分:0)
我没有花时间真正清理掉它或其他任何东西,但直到collection(5)
为止,它都可以正常工作。这里有很多需要改进的地方,但是我会让其他人进行调整和编辑以帮助您学习。现在起作用,然后形成。
Sub fullPageLine()
Dim rng As Range
Dim cht As Object
'Data range for the chart
Set rng = Selection
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
'Create a chart
Set cht = Selection
'Give chart some data
'cht.Chart.SetSourceData Source:=rng
'Reposition Title
With cht
.Left = 24.632
.Top = 6
End With
'Format x axis
ActiveChart.ChartArea.Select
With Selection
.Format.TextFrame2.TextRange.Font.Name = "Arial"
.Format.TextFrame2.TextRange.Font.Size = 7
End With
'Format title
ActiveChart.ChartTitle.Font.Size = 8.4
ActiveChart.ChartTitle.Left = 23.632
ActiveChart.ChartTitle.Top = 6
'Format legend
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
With ActiveChart.FullSeriesCollection(2).Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent2
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
End With
With ActiveChart.FullSeriesCollection(3).Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
End With
With ActiveChart.FullSeriesCollection(4).Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.5
.Transparency = 0
End With
With ActiveChart.FullSeriesCollection(5).Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.349999994
.Transparency = 0
End With
End Sub