使用 VBA 将多个数据系列添加到 Excel 图表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10002624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:53:18  来源:igfitidea点击:

Add multiple data series to excel chart with VBA

excelchartsexcel-vbavba

提问by Martin H

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 范围(日期),我可以做

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!:"

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.

一个潜在的答案不需要坚持我上面发布的内容,欢迎提出任何建议。

回答by bouvierr

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 列作为 Y1,D 列作为 Y2:

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