如何纯粹通过excel vba编程创建堆积柱形图?
我正在尝试在 VBA 中创建一个堆积柱形图,其中有一个条形图,每个条形图显示图表上子类别的细分(参见图片右侧)。对于普通条形图,我所要做的就是创建一个新系列并设置 Values 和 XValues 属性。我不知道如何告诉它子类别的值。
(来源:msecnd.net)
I am trying to create a stacked column chart in VBA where there is a bar graph and each bar graph shows the breakdown of subcategories on the graph (See right side of picture). For the normal bar graph all I had to do was create a new series and set the Values and XValues properties. I'm not sure how to tell it about the values for the subcategories.
(source: msecnd.net)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
启动此类任务的最简单方法是查看手动执行任务时会发生什么,使用宏记录器捕获 VBA 等效项。
例如,我刚刚记录了这个(在Excel XP中,但以后的版本应该类似)。代码与记录的完全一样:
查看上面的内容,我想说第一行
Range("B4:D6").Select
是不需要的,因为它会进一步重复。现在要清理一下。在这里,对象浏览器非常有用。如果您不熟悉,请尝试在 VBA 编辑器中按 F2。通过在上部下拉列表中选择“Excel”稍微缩小上下文范围,然后查看“类”列表中的“图表”。在“Members...”列表中选择
Add
会在下部窗格中显示以下内容:因此,
Charts.Add
返回一个图表,它必须是ActiveChart
code> 在代码的其余部分中重复引用。像这样的重复很乏味(并且可能会影响性能),所以我会稍微修改一下。为了便于练习,我们假设我们将在运行宏之前选择目标数据,因此当宏运行时它将位于Selection
中。查看对象浏览器中 Chart 的成员,很明显SetSourceData
中的Source
应该是Range
。让我们试试这个:它在我的机器上不起作用 - 我认为激活图表时
选择
可能会发生变化。因此,我们应该在开始使用图表之前捕获范围:从那里,您应该能够找到各种自定义(记住对象浏览器!)以按照您想要的方式获取图表。 (请记住,根据您的 Excel 版本,可能有一些小功能无法通过 VBA 访问。)
The simplest way to start a task like this is to see what happens when you perform the task by hand, using the Macro Recorder to capture the VBA equivalent.
For example, I just recorded this (in Excel XP, but later versions should be similar). The code is exactly as recorded:
Looking at the above, I'd say the first line,
Range("B4:D6").Select
isn't needed, since it's repeated further down.Now to clean up a bit. Here the Object Browser can be hugely useful. If you're not familiar, try hitting F2 in the VBA editor. Narrow down the context a little by selecting "Excel" in the upper dropdown and look at Charts in the Classes list. Selecting
Add
in the "Members..." list gives this in the lower pane:So
Charts.Add
returns a Chart, which must be theActiveChart
that's repeatedly referenced in the remainder of the code. Duplication like that is tedious (and can be performance-affecting) so I'd rework a little. Let's assume for the sake of the exercise that we're going to select our target data before running the macro, so it'll be inSelection
when the macro runs. Looking at the members of Chart in the Object Browser, it's apparent thatSource
inSetSourceData
should be aRange
. Let's try this:It doesn't work on my machine - I think
Selection
probably changes when a chart is activated. So we should capture the range before starting to work with the chart:From there, you should be able to find all kinds of customizations (remember Object Browser!) to get the chart how you want it. (Bear in mind that, depending on your Excel version, there may be a few minor features not accessible via VBA.)
要创建堆积柱效果,您需要为柱中的每个细分创建一个系列。例如,假设我们有 4 个动物园。假设他们位于迈阿密、亚特兰大、纽约和拉斯维加斯。此外,我们还有 4 种动物:狮子、老虎、熊和海豹。我们想要一个堆积的列,显示每只动物的数量以及动物的总数。
首先,将图表类型设置为 xl3DColumnStacked,如下所示。
然后我们将为每种动物创建一个系列。
然后将 Values 属性设置为动物值数组,将 XValues 属性设置为城市名称数组。因此,如果第一个系列是针对狮子队的,请创建一个包含迈阿密、亚特兰大、纽约、拉斯维加斯的狮子数量的数组,以及包含字符串“迈阿密”、“亚特兰大”等的数组。cityNames 数组将被重复用于每个系列系列,但 lionCounts 显然每次都会被替换为适当的数组。即 TigerCounts、bearCounts 等。
这应该是使用直接来自程序的值而不是单元格引用创建堆叠列所需的全部内容。
To create the stacked column effect, you need to create a series for each subdivision in the column. For example, say we had 4 zoos. Say they are located in Miami, Atlanta, New York, and Las Vegas. Also, we have 4 animals, Lions, Tigers, Bears, and Seals. And we want a stacked column showing the number of each animal and also the total number of animals.
First, set the chart type to xl3DColumnStacked like this.
We would then create a series for each animal.
Then set the Values property to the array of values for the animal and the XValues property to the array of names for the cities. So if the first series was for Lions, make an array with the # of lions at Miami, Atlanta, NY, Las Vegas and an array containing the strings "Miami","Atlanta", etc. The cityNames array will be reused for each Series but the lionCounts will obviously be replaced each time with the appropriate array. i.e. tigerCounts, bearCounts, etc.
That should be all it takes to create a stacked column using values straight from your program instead of cell references.
手动录制宏时,图表类型有两种可能:一个按行绘制,另一个按列绘制。如果稍后由其他进程更改此值,则宏不知道将其切换回来。
为了确保每次都正确显示,请指定
ActiveChart.SetSourceData
值,例如ActiveChart.SetSourceData Range("A1:B2")、xlRows
或
ActiveChart.SetSourceData范围(“A1:B2”),xlColumns
When recording the macro manually, two possibilities are offered for the ChartType; one plots by rows, the other by columns. If this is changed later by some other process, the macro doesn't know to switch it back.
To ensure that it is displayed properly each time specify the
ActiveChart.SetSourceData
value e.g.ActiveChart.SetSourceData Range("A1:B2"), xlRows
or
ActiveChart.SetSourceData Range("A1:B2"), xlColumns