我正在使用 VBA 在我在 Excel 2010 中创建的程序中绘制数据。我将其发送到另一台具有 Excel 2013 的计算机上,我发现除了这个图形问题之外,一切都运行良好。
我的代码将创建图表,并完美地调整它们的大小,但它实际上不会绘制任何数据点。在我的代码中,我还添加和删除了系列,我注意到系列的数量在侧面是正确的,但系列没有保留我给它们的自定义名称。
但这就是转折点。当我右键单击图表并单击“选择数据”时,所有数据都会立即弹出,包括我为该系列自定义的所有名称。我什至不进入选择数据或任何东西。当我点击它的那一刻,所有的值都会立即弹出。
Here is an imgur album of what it looks like before/after. Note that I do absolutely nothing other than right click and select the option "select data".就像数据已经被选中,但直到我点击“选择数据”才显示出来。
为什么 Excel 2013 会这样做?如何使这些值通过 VBA 方法实际显示?我将在下面添加我使用的图形代码示例。
'Setting the range the chart will cover
Set rngChart = ActiveSheet.Range(Cells(Counter + 3, 4), Cells(Counter + 27, 10))
'Dimensioning the chart and choosing chart type
Set co = ActiveSheet.Shapes.AddChart(xlXYScatter, rngChart.Cells(1).Left, rngChart.Cells(1).Top, rngChart.Width, rngChart.Height)
Set cht = co.Chart
Set sc = cht.SeriesCollection
'Remove any default series
Do While sc.Count > 0
sc(1).Delete
Loop
'Setting chart data
'Series 1
With sc.NewSeries
.Name = "=Sheet1!$C$1"
.XValues = "=Sheet2!$A$2:$A$" & SimpleTracker + 1
.Values = "=Sheet2!$B$2:$B$" & SimpleTracker + 1
.MarkerSize = 3
.MarkerStyle = xlMarkerStyleCircle
End With
'Series 2
With sc.NewSeries
.Name = "=Sheet1!$B$1"
.XValues = "=Sheet1!$A$2:$A$" & Counter + 1
.Values = "=Sheet1!$B$2:$B$" & Counter + 1
.MarkerSize = 5
.MarkerStyle = xlMarkerStyleCircle
.MarkerBackgroundColorIndex = 10
.MarkerForegroundColorIndex = 10
End With
'Setting chart labels
With cht
.HasTitle = True
If n = 0 Then
.ChartTitle.Characters.Text = "Simple Fit - CFL Over Time"
ElseIf Range("I" & n) = "Regression Title" Then
.ChartTitle.Characters.Text = Range("J" & n).Text
Else
.ChartTitle.Characters.Text = "Simple Fit - CFL Over Time"
End If
.Axes(xlCategory, xlPrimary).HasTitle = True
If DayTracker = 1 Then
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (Days)"
ElseIf HourTracker = 1 Then
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (Hours)"
Else
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (Minutes)"
End If
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "CFL"
.Axes(xlCategory).HasMajorGridlines = True
.Axes(xlCategory).HasMinorGridlines = True
End With
我还想指出,此代码在 Excel 2010 中仍然可以完美运行,并且图形仍然可以在那里工作。它只是在 Excel 2013 中不起作用。
感谢您的阅读!如果您有任何问题或需要任何澄清,请告诉我。
最佳答案
代替
With sc.NewSeries
和
With cht.SeriesCollection.NewSeries
我在 Excel 2013 中遇到了同样的问题。我不知道为什么,但这个解决方案对我有用。
关于vba - 数据在 Excel 2010 中绘制,但在 Excel 2013 中没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24171492/