简体   繁体   English

使用VBA将Datapoint添加到Excel图表

[英]Adding Datapoint to Excel Chart with VBA

I have a chart object, with 3 series. 我有一个图表对象,有3个系列。 The series are getting the Y-values from ranges C1:C10, D1:D10 and E1:E10. 该系列从C1范围获得Y值:C10,D1:D10和E1:E10。 The value depends on the values in A1:A10 (eg C1 = A1+6); 该值取决于A1:A10中的值(例如C1 = A1 + 6); but I'm charting the values against the values in B1:10 (its a Log-Normal graph). 但是我正在根据B1:10中的值绘制值(它是一个Log-Normal图)。

I'm calculating the values in VBA. 我正在计算VBA中的值。 Since there is only a discrete number of points in A1:A10, I'd like to add some extra points of interest to the chart. 由于A1:A10中只有一些离散的点,我想在图表中添加一些额外的兴趣点。 So if A1:A10 contains the integers 1 to 10, I'd like to plot a decimal number like 3.5, without having to add any new rows to the worksheet. 因此,如果A1:A10包含整数1到10,我想绘制一个像3.5这样的十进制数,而不必在工作表中添加任何新行。

From looking around, I'd assume it would be something with the Extend method ( MSDN - Extend Method ) but I'm not sure how to: 从四处看看,我认为它将是Extend方法( MSDN - 扩展方法 ),但我不知道如何:

  1. Extend a specific series (like only add a point to series C1:C10 and D1:D10 扩展一个特定的系列(比如只添加C1系列:C10和D1:D10
  2. How to add a data point without requiring to add a cell to the worksheet. 如何添加数据点而无需将单元格添加到工作表。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

Question 2 问题2

You can set the values on an individual series by using the value property on the series object. 您可以使用系列对象上的value属性设置单个系列的值。

However, in Help, it states that the values in a series can either be 但是,在“帮助”中,它指出系列中的值可以是

a range on a worksheet or an array of constant values , 工作表上范围常量值数组

but not both . 但不是两个

This means that if you want to specify the series values as a range such as C1:C10, then I think you'll have to add cells if you want to add data points in the series. 这意味着如果要将系列值指定为C1:C10等范围,那么我认为如果要在系列中添加数据点,则必须添加单元格。

If you don't want to add a cell, then you have to specify all values as an array constant. 如果您不想添加单元格,则必须将所有值指定为数组常量。

Question 1 问题1

To add data points to a specific series, I believe you would have to select the series, and modify the Values and XValues properties. 要将数据点添加到特定系列,我相信您必须选择系列,并修改XValues属性。

Example: 例:

Put this data in the "Sheet1" of Excel and graph it as "Chart1". 将此数据放在Excel的“Sheet1”中,并将其绘制为“Chart1”。 y1 will be series 1, y2 will be series2 and y3 will be series 3. y1将是系列1,y2将是series2,y3将是系列3。

     A    B     C     D
1    x    y1    y2    y3
2    1    10    100   400
3    2    20    200   500
4    3    30    300   600

Now, lets add a data point to y2. 现在,让我们将数据点添加到y2。

     A    B     C     D
1    x    y1    y2    y3
2    1    10    100   400
3    2    20    200   500
4    3    30    300   600
5    4          1000

We have to select the series (by number or by name, in this case, 2 or "y2") and set the Value property to "C2:C5" 我们必须选择系列(按编号或按名称,在本例中为2或“y2”)并将Value属性设置为“C2:C5”

'using ranges
Charts("chart1").SeriesCollection("y2").Values = Worksheets("Sheet1").Range("C2:C5")

'using array constant
Charts("chart1").SeriesCollection("y2").Values = Array(100, 200, 300, 1000)

We'll also change the XValues property so that every Value has an XValue 我们还将更改XValues属性,以便每个Value都有一个XValue

'using ranges
Charts("chart1").SeriesCollection("y2").XValues = Worksheets("Sheet1").Range("A2:A5")

'using array constant
Charts("chart1").SeriesCollection("y2").XValues = Array(1, 2, 3, 4)

Note: 注意:

We can have Values as a range and XValues as an array constant or vice versa. 我们可以将值作为范围,将XValues作为数组常量,反之亦然。

We can also have both Values and XValues as ranges or both as array constants. 我们也可以将Values和XValues作为范围或两者作为数组常量。

We cannot have Values as a range and an array constant. 我们不能将Values作为范围和数组常量。

We cannot have XValues as a range and an array constant. 我们不能将XValues作为范围和数组常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM