我正在尝试使用VBA和图表对象在Excel中创建图表。使用下面的代码我不断得到错误:“对象变量或没有设置块变量”我不完全确定这意味着什么,并将非常感谢一些指导。谢谢!
Dim newChart As Chart
Private Sub test2_Click()
With newChart
.ChartType = xlColumnClustered
.Location Where:=xlLocationAsNewSheet, Name:="Random chart"
.SeriesCollection.NewSeries
.FullSeriesCollection(1).Name = "month1"
.FullSeriesCollection(1).Value = Range("A1:A12")
.SeriesCollection.NewSeries
.FullSeriesCollection(2).Name = "month2"
.FullSeriesCollection(2).Value = Range("A1:A12")
.ChartTitle.Text = "random chart"
End With
End Sub
答案 0 :(得分:0)
查看ChartObjects.Add
方法。如果您打算创建一个由newChart
变量表示的新图表,您需要告诉应用程序创建图表,因为它无法读懂您的想法:)
http://msdn.microsoft.com/en-us/library/office/ff197613(v=office.15).aspx
Set newChart = ActiveSheet.ChartObjects.Add(50,50,400,300).Chart
With newChart
.ChartType = xlColumnClustered
.Location Where:=xlLocationAsNewSheet, Name:="Random chart"
.SeriesCollection...