我的学校考试项目有任务,我已经尝试过并希望你能帮助我
问题是这样的:
由于“延迟”的学生构成了大学的“风险”,因此研究主任必须做好准备 关于这组学生的一些额外统计数据。创建一个可以绘制以下i的子/函数 表格“延迟学生”:
研究主管希望能够通过按下“延迟学生”表上的按钮来生成统计数据。
这是我的代码,它不想工作:
Set rng = ActiveSheet.Range ("A2: I2")
Set cht = ActiveSheet.Shapes.AddChart2
cht.Chart.SetSourceData Source: = rng
cht.Chart.Chartype = xlXYScatterLines
cht.Chart.ChartTitle.Text = "Statistic of Student"
答案 0 :(得分:0)
您必须将rng
和cht
声明为对象。这有效:
Public Sub TestMe()
Dim rng As Object, cht As Object
Set rng = ActiveSheet.Range("A2: I2")
Set cht = ActiveSheet.Shapes.AddChart2
With cht.Chart
.SetSourceData Source:=rng
.ChartType = xlXYScatterLines
.ChartTitle.Text = "Statistic of Student"
End With
End Sub
答案 1 :(得分:0)
我找到了这个MSDN页面,它解释了如何通过VBA创建图形:
https://msdn.microsoft.com/en-us/library/office/aa203725(v=office.11).aspx
他们页面的代码:
Sub AddChartSheet()
Dim chtChart As Chart
'Create a new chart.
Set chtChart = Charts.Add
With chtChart
.Name = "Tool Sales2"
.ChartType = xlColumnClustered
'Link to the source data range.
.SetSourceData Source:=Sheets("Sheet1").Range("A1:D5"), _
PlotBy:=xlRows
.HasTitle = True
.ChartTitle.Text = "=Sheet1!R1C2"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Month"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Sales"
End With
End Sub
这样可以消除您的错误:
Dim cht As Chart
Dim rng as Range
Set rng = ActiveSheet.Range ("A2: I2")
Set cht = ActiveSheet.Shapes.AddChart2
cht.Chart.ChartType = xlXYScatterLines
cht.Chart.SetSourceData Source:= rng, PlotBy:=xlRows
cht.Chart.ChartTitle.Text = "Statistic of Student"