简体   繁体   English

使用VBA将其他工作簿中的图表数据添加到一个系列中-Excel 2007

[英]Add Chart Data from other Workbooks into one series with VBA - Excel 2007

I need to create a Chart which will grab data from external sources when a macro is run. 我需要创建一个图表,当运行宏时它将从外部源获取数据。 Setting up the UserForm and all that is fine, all done. 设置UserForm并完成所有工作。 However, I can't find out how to add another piece of the series. 但是,我找不到如何添加该系列的另一部分。

Is it even possible to have a series that sources data from several different locations? 甚至可能有一系列从多个不同位置获取数据的数据吗? Basically I need it to go in chronological order (Horizontal Axis are all dates), but each date comes from a different workbook entirely. 基本上,我需要按时间顺序排列(水平轴都是日期),但是每个日期完全来自不同的工作簿。 I have tried to get Excel to do this with its basic functions. 我试图让Excel用其基本功能来做到这一点。 I just can't get it to put it into the graph. 我只是无法将其放入图表中。 If possible, I would like to not have to copy all the reference data to the workbook with the graphs, as it is already going to have 16+ Charts that will be on their own sheet. 如果可能的话,我不想将所有参考数据都与图表一起复制到工作簿中,因为已经有16张以上的图表将出现在它们自己的图纸上。

Does anyone know how to make Excel accept many different references into one series? 有谁知道如何使Excel在一个系列中接受许多不同的引用? How it would be done with VBA? 使用VBA怎么办? I'm not too bad with VBA, just haven't had to deal with Charting before. 我对VBA不太满意,只是之前不必处理Charting。

You cannot combine data from different worksheets into the values of a series, or into its Xvalues. 您不能将来自不同工作表的数据合并到一个系列的值或X值中。 (You can use data from one sheet for X and another for Y). (您可以将一张中的数据用于X,另一张中的数据用于Y)。

I recommend setting up a chart data worksheet, then copy the data from each of your data sheets and pasting it in this sheet. 我建议设置一个图表数据工作表,然后从每个数据表中复制数据并将其粘贴到此表中。

The problem with using arrays for the data is that the length of the array is limited to around 250 characters. 使用数组存储数据的问题在于数组的长度限制为大约250个字符。 Including commas and assuming only a 2 digit number with no decimal digits, this means you get only 80 values. 包括逗号并假设仅2位数字,没有小数位,这意味着您只能获得80个值。 If you have floating point values, you will be lucky to get more than about 15 values. 如果您有浮点值,那么您将很幸运获得超过15个值。

I don't think you can have a single axis of a single series sourced from multiple ranges/addresses, but you can write VBA to set the values of a chart series to specific values rather than to a contiguous range. 我不认为您可以有一个来自多个范围/地址的单个系列的单个轴,但是您可以编写VBA将图表系列的值设置为特定值,而不是一个连续范围。 Here's an example from the help: 这是帮助中的示例:

Charts("Chart1").SeriesCollection(1).Values = Array(1, 3, 5, 7, 11, 13, 17, 19)
Charts("Chart1").SeriesCollection(1).XValues = Array(5.0, 6.3, 12.6, 28, 50)

You would have to write code to read the values out of the ranges in your different workbooks, but this should work. 您将必须编写代码以读取不同工作簿中范围之外的值,但这应该可以工作。

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

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