使用VBA将多个数据序列添加到excel图表中

时间:2022-10-01 12:32:53

Assuming I have my data in rows and I want to create a chart (x,y scatter) from it.

假设我的数据是行,我想要从中创建一个图表(x,y散射)。

date    1.1. 1.2. 1.3. 1.4 1.5
set1    12  36  12  23  33
set2    44  22  11  1   13
set3    54  5   56  56  34
set4    1   2   6   12  33

how can I add set1,set2 and set4 but not set3 to the chart without having to select them individually one after another?

如何向图表中添加set1、set2和set4而不是set3,而不必逐个地选择它们?

For the x-range (date) I can do

对于x-range(日期)我可以做

Dim xrange As Range
Set xrange = Application.InputBox("select x-range", "Get Range", Type:=8)

But doing the same for the data sets (selection of multiple rows) won't work because each series in a scatter plot requires a individual range

但是对数据集(选择多行)做同样的操作是行不通的,因为散点图中的每个系列都需要一个单独的范围

   ActiveChart.SeriesCollection(1).XValues = xrange
   ActiveChart.SeriesCollection(1).Values = "=Sheet1!$4:$4"

but I have a selection of multiple datasets. I would need a way to split the ranges from my input box selection(s) and to write them to unique variables. Then I would add a new series for each xrange + variable pair with the above method.

但是我有多个数据集的选择。我需要一种方法来将这些范围从我的输入框选择中分离出来,并将它们写到唯一的变量中。然后,我将使用上述方法为每个xrange +变量对添加一个新的系列。

Is there a wayto do this?

有办法做到这一点吗?

A potentail answer does not need to stick to what I posted above, any suggestions are welcome.

如果你的回答不符合我上面所写的内容,那么任何建议都是受欢迎的。

1 个解决方案

#1


4  

Set the source data range. Excel will automatically create the series for you. In the example below I have column A as X, column B as Y1 and column D as Y2:

设置源数据范围。Excel将自动为您创建这个系列。在下面的例子中,A列为X, B列为y, D列为y:

ActiveChart.SetSourceData Source:=Range("A1:B4,D1:D4")

#1


4  

Set the source data range. Excel will automatically create the series for you. In the example below I have column A as X, column B as Y1 and column D as Y2:

设置源数据范围。Excel将自动为您创建这个系列。在下面的例子中,A列为X, B列为y, D列为y:

ActiveChart.SetSourceData Source:=Range("A1:B4,D1:D4")