简体   繁体   English

来自VBA而不是单元格的图表数据

[英]Chart Data from VBA not Cells

I want to know if it is possible to set the data to a chart from VBA code without having to set it to a bunch of cells. 我想知道是否可以从VBA代码中将数据设置为图表,而不必将其设置为一堆单元格。

For the purpose, it can plot anything.. even random numbers. 为此,它可以绘制任何内容,甚至随机数。 But I don't want to use any cells to paste the values and then do the chart get these values. 但是我不想使用任何单元格来粘贴值,然后让图表获取这些值。

[My application has a Sheet with a lot of letters "M" (maintenance), "O" (opperating) and "A" (available), for everyday of the year, from a bunch of equipments. [我的应用程序在一年中的每一天都有一堆来自许多设备的工作表,其中包含许多字母“ M”(维护),“ O”(操作)和“ A”(可用)。 I do some vba filtering on the equipments to get some that I want to plot and want to do some calculations based on the "M", "O" and "A" quantities.] 我在设备上进行了一些vba过滤,以获取要绘制的图像,并希望基于“ M”,“ O”和“ A”数量进行一些计算。]

Here's a simple example: 这是一个简单的例子:

Sub AddChart()
    Dim cht     As Chart
    Dim ser     As Series

    Set cht = Charts.Add
    cht.ChartType = xlColumnClustered
    Set ser = cht.SeriesCollection.NewSeries
    ser.XValues = Array(1, 3, 5, 7, 9)
    ser.Values = Array(2.4, 3.2, 5.7, 12.67)
End Sub

You must note that the SERIES formula for a chart in some versions (2003 and prior, I think) is limited to 1024 characters. 您必须注意,某些版本(我认为2003年及以前)中图表的SERIES公式限制为1024个字符。 In 2010, it seems to be limited to 8192 characters. 在2010年,似乎仅限于8192个字符。 That means that if you use literal values (rather than a range) for the data, each character is included in the formula - so the more decimal places you specify and the more data points, the more likely the code will fail. 这意味着,如果对数据使用文字值(而不是范围),则每个字符都包含在公式中-因此,您指定的小数位数越多,数据点越多,则代码失败的可能性就越大。

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

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