一张新图表 [英] A new chart every sheet
问题描述
我正在尝试根据我已经拥有的一些信息添加一张新的图表。但是,最后一张信息的图表最后只能一张。我认为图表是自己覆盖的。
I'm trying to add a new chart every sheet based on some information I already have. However, I end up having just one sheet with the chart for the last piece of information. I think the charts are overwriting themselves.
这里是代码
Dim chart1 As chart
Set chart1 = Charts.Add
For i = 0 To 9
chart1.Add.SetSourceData Source:=Destino.Range("A24").Offset(0, 3 * i).CurrentRegion, PlotBy:=xlRows
chart1.ChartType = xlBarClustered
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="gráfico" & i + 1
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Cuenta por categoría de" & origen.[c4].Offset(i, 0)
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Categoría"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Frecuencia"
End With
ActiveSheet.Move After:=Sheets(ActiveWorkbook.Sheets.Count)
Next
推荐答案
在您的示例中,您声明并将该对象设置在循环外。所以在循环的每个迭代中它都是一样的Chart实例,它基本上是被覆盖或者更准确的更新。
In your example, you declare and set the Chart object outside of the loop. So it's the same instance of Chart in each iteration of the loop, and it does essentially get over-written, or more accurately, updated.
尝试在循环中移动它们,并将Charts对象声明为New。现在每次它增量,它将生成一个新的图表实例。
Try moving them inside the loop, and declare the Charts object as New. Now each time it increments, it will generate a NEW instance of Chart. Otherwise, everything looks good.
For i = 0 To 9
Dim chart1 As New Chart
Set chart1 = Charts.Add
chart1.Add.SetSourceData Source:=Destino.Range("A24").Offset(0, 3 * i).CurrentRegion, PlotBy:=xlRows
chart1.ChartType = xlBarClustered
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="gráfico" & i + 1
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Cuenta por categoría de" & origen.[c4].Offset(i, 0)
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Categoría"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Frecuencia"
End With
ActiveSheet.Move After:=Sheets(ActiveWorkbook.Sheets.Count)
Next
这篇关于一张新图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!