如何纯粹通过excel vba编程创建堆积柱形图?

发布于 2024-09-08 14:22:31 字数 406 浏览 7 评论 0原文

我正在尝试在 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.

alt text
(source: msecnd.net)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鯉魚旗 2024-09-15 14:22:31

启动此类任务的最简单方法是查看手动执行任务时会发生什么,使用宏记录器捕获 VBA 等效项。

例如,我刚刚记录了这个(在Excel XP中,但以后的版本应该类似)。代码与记录的完全一样:

Option Explicit

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 09/07/2010 by MW
'

'
    Range("B4:D6").Select
    Charts.Add
    ActiveChart.ChartType = xlColumnStacked
    ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("B4:D6"), PlotBy:= _
        xlRows
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
End Sub

查看上面的内容,我想说第一行 Range("B4:D6").Select 是不需要的,因为它会进一步重复。

现在要清理一下。在这里,对象浏览器非常有用。如果您不熟悉,请尝试在 VBA 编辑器中按 F2。通过在上部下拉列表中选择“Excel”稍微缩小上下文范围,然后查看“类”列表中的“图表”。在“Members...”列表中选择 Add 会在下部窗格中显示以下内容:

Function Add([Before], [After], [Count]) As Chart
    Member of Excel.Charts

因此,Charts.Add 返回一个图表,它必须是 ActiveChart code> 在代码的其余部分中重复引用。像这样的重复很乏味(并且可能会影响性能),所以我会稍微修改一下。为了便于练习,我们假设我们将在运行宏之前选择目标数据,因此当宏运行时它将位于 Selection 中。查看对象浏览器中 Chart 的成员,很明显 SetSourceData 中的 Source 应该是 Range。让我们试试这个:

Sub Macro1()
    With Charts.Add
        .ChartType = xlColumnStacked
        .SetSourceData Source:=Selection, PlotBy:=xlRows
        .Location Where:=xlLocationAsObject, Name:="Sheet1"
    End With
End Sub

它在我的机器上不起作用 - 我认为激活图表时选择可能会发生变化。因此,我们应该在开始使用图表之前捕获范围:

Sub CreateStackedColumnChartFromSelection()
Dim src As Range
    Set src = Selection
    With Charts.Add
        .ChartType = xlColumnStacked
        .SetSourceData Source:=src, PlotBy:=xlRows
        .Location Where:=xlLocationAsObject, Name:="Sheet1"
    End With
End Sub

从那里,您应该能够找到各种自定义(记住对象浏览器!)以按照您想要的方式获取图表。 (请记住,根据您的 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:

Option Explicit

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 09/07/2010 by MW
'

'
    Range("B4:D6").Select
    Charts.Add
    ActiveChart.ChartType = xlColumnStacked
    ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("B4:D6"), PlotBy:= _
        xlRows
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
End Sub

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:

Function Add([Before], [After], [Count]) As Chart
    Member of Excel.Charts

So Charts.Add returns a Chart, which must be the ActiveChart 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 in Selection when the macro runs. Looking at the members of Chart in the Object Browser, it's apparent that Source in SetSourceData should be a Range. Let's try this:

Sub Macro1()
    With Charts.Add
        .ChartType = xlColumnStacked
        .SetSourceData Source:=Selection, PlotBy:=xlRows
        .Location Where:=xlLocationAsObject, Name:="Sheet1"
    End With
End Sub

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:

Sub CreateStackedColumnChartFromSelection()
Dim src As Range
    Set src = Selection
    With Charts.Add
        .ChartType = xlColumnStacked
        .SetSourceData Source:=src, PlotBy:=xlRows
        .Location Where:=xlLocationAsObject, Name:="Sheet1"
    End With
End Sub

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.)

云柯 2024-09-15 14:22:31

要创建堆积柱效果,您需要为柱中的每个细分创建一个系列。例如,假设我们有 4 个动物园。假设他们位于迈阿密、亚特兰大、纽约和拉斯维加斯。此外,我们还有 4 种动物:狮子、老虎、熊和海豹。我们想要一个堆积的列,显示每只动物的数量以及动物的总数。

首先,将图表类型设置为 xl3DColumnStacked,如下所示。

ActiveChart.ChartType = xl3DColumnStacked

然后我们将为每种动物创建一个系列。

ActiveChart.SeriesCollection.NewSeries

然后将 Values 属性设置为动物值数组,将 XValues 属性设置为城市名称数组。因此,如果第一个系列是针对狮子队的,请创建一个包含迈阿密、亚特兰大、纽约、拉斯维加斯的狮子数量的数组,以及包含字符串“迈阿密”、“亚特兰大”等的数组。cityNames 数组将被重复用于每个系列系列,但 lionCounts 显然每次都会被替换为适当的数组。即 TigerCounts、bearCounts 等。

ActiveChart.SeriesCollection(1).Values = lionCounts
ActiveChart.SeriesCollection(1).XValues = cityNames

这应该是使用直接来自程序的值而不是单元格引用创建堆叠列所需的全部内容。

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.

ActiveChart.ChartType = xl3DColumnStacked

We would then create a series for each animal.

ActiveChart.SeriesCollection.NewSeries

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.

ActiveChart.SeriesCollection(1).Values = lionCounts
ActiveChart.SeriesCollection(1).XValues = cityNames

That should be all it takes to create a stacked column using values straight from your program instead of cell references.

明月松间行 2024-09-15 14:22:31

手动录制宏时,图表类型有两种可能:一个按行绘制,另一个按列绘制。如果稍后由其他进程更改此值,则宏不知道将其切换回来。

为了确保每次都正确显示,请指定 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文