动态Excel图表不显示所有数据

时间:2014-09-09 02:53:35

标签: excel excel-vba excel-charts vba

我有这个代码,允许用户在一些单元格中输入图表参数并动态创建图表。许多系列(最多四个)允许在两个垂直(y)轴和一个共用水平(x)轴上。图表通常是列和线的混合,数据范围的长度各不相同。我有这样的代码添加了这样的系列(我将尝试坚持我认为相关的代码)

 seriesCount = 1
 If hasSeries1 = True Then
   ActiveChart.SeriesCollection.NewSeries
   ActiveChart.SeriesCollection(seriesCount).Name = .Cells(2, 6) & " " & axisside1
   ActiveChart.SeriesCollection(seriesCount).ChartType = chartType1
   ActiveChart.SeriesCollection(seriesCount).AxisGroup = axisgroup1
   ActiveChart.SeriesCollection(seriesCount).Border.LineStyle = borderStyle1
   ActiveChart.SeriesCollection(seriesCount).Border.Color = lineColor1
   ActiveChart.SeriesCollection(seriesCount).Format.Line.Weight = lineWidth1
   ActiveChart.SeriesCollection(seriesCount).Format.Fill.ForeColor.RGB = seriesColor1
   ActiveChart.SeriesCollection(seriesCount).Format.Line.Visible = hasLine1
   ActiveChart.SeriesCollection(seriesCount).XValues = dates1
   ActiveChart.SeriesCollection(seriesCount).Values = dataset1
   seriesCount = seriesCount + 1
 End If
 If hasSeries2 = True Then
   ActiveChart.SeriesCollection.NewSeries
   ActiveChart.SeriesCollection(seriesCount).Name = .Cells(3, 6) & " " & axisside2
   ActiveChart.SeriesCollection(seriesCount).ChartType = chartType2
   ActiveChart.SeriesCollection(seriesCount).AxisGroup = axisgroup2
   ActiveChart.SeriesCollection(seriesCount).Border.LineStyle = borderStyle2
   ActiveChart.SeriesCollection(seriesCount).Border.Color = lineColor2
   ActiveChart.SeriesCollection(seriesCount).Format.Line.Weight = lineWidth2
   ActiveChart.SeriesCollection(seriesCount).Format.Fill.ForeColor.RGB = seriesColor2
   ActiveChart.SeriesCollection(seriesCount).Format.Line.Visible = hasLine2
   ActiveChart.SeriesCollection(seriesCount).XValues = dates2
   ActiveChart.SeriesCollection(seriesCount).Values = dataset2
   seriesCount = seriesCount + 1
 End If
 If hasSeries3 = True Then
   ActiveChart.SeriesCollection.NewSeries
   ActiveChart.SeriesCollection(seriesCount).Name = .Cells(4, 6) & " " & axisside3
   ActiveChart.SeriesCollection(seriesCount).ChartType = chartType3
   ActiveChart.SeriesCollection(seriesCount).AxisGroup = axisgroup3
   ActiveChart.SeriesCollection(seriesCount).Border.LineStyle = borderStyle3
   ActiveChart.SeriesCollection(seriesCount).Border.Color = lineColor3
   ActiveChart.SeriesCollection(seriesCount).Format.Line.Weight = lineWidth3
   ActiveChart.SeriesCollection(seriesCount).Format.Fill.ForeColor.RGB = seriesColor3
   ActiveChart.SeriesCollection(seriesCount).Format.Line.Visible = hasLine3
   ActiveChart.SeriesCollection(seriesCount).XValues = dates3
   ActiveChart.SeriesCollection(seriesCount).Values = dataset3
   seriesCount = seriesCount + 1
 End If
 If hasSeries4 = True Then
   ActiveChart.SeriesCollection.NewSeries
   ActiveChart.SeriesCollection(seriesCount).Name = .Cells(5, 6) & " " & axisside4
   ActiveChart.SeriesCollection(seriesCount).ChartType = chartType4
   ActiveChart.SeriesCollection(seriesCount).AxisGroup = axisgroup4
   ActiveChart.SeriesCollection(seriesCount).Border.LineStyle = borderStyle4
   ActiveChart.SeriesCollection(seriesCount).Border.Color = lineColor4
   ActiveChart.SeriesCollection(seriesCount).Format.Line.Weight = lineWidth4
   ActiveChart.SeriesCollection(seriesCount).Format.Fill.ForeColor.RGB = seriesColor4
   ActiveChart.SeriesCollection(seriesCount).Format.Line.Visible = hasLine4
   ActiveChart.SeriesCollection(seriesCount).XValues = dates4
   ActiveChart.SeriesCollection(seriesCount).Values = dataset4
 End If

问题出在这里:图表只显示它应该的部分数据。当我右键单击数据系列时,点击Select Data并选择Edit,正确的系列(x和y)都会突出显示,但显示的内容是截断的子集应该是什么

以下是我所看到的内容

的示例

myChart

以下是浅蓝色列的一些数据

12/30/2005       307%
1/31/2006        302%
2/28/2006        248%
3/31/2006        262%
4/28/2006        285%
5/31/2006        256%
...              ...
...              ...
...              ...
6/30/2014        147%

请注意这些数据应该如何在2005年12月30日开始的图表上显示,但是从2013年11月30日开始(尽管数值看起来是正确的,307%,302%,等等。)。几乎就像excel强制第二和第三数据系列与第一个数据系列一样长。第一个是正确的图表。

1 个答案:

答案 0 :(得分:0)

我认为你没有使用XY图表,因此每个系列必须有相同的标签(Xvalues =日期)。这意味着您需要创建一个包含所有日期的唯一日期 - 设置并将其(作为Xvalues)分配给第一个系列。