将VBA宏写入图形

时间:2014-07-22 16:02:47

标签: excel vba excel-vba graph charts

我正在尝试使用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

1 个答案:

答案 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...