vba excel图表中的类别名称,使用VBA而不使用任何单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24559775/
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-12 03:40:57  来源:igfitidea点击:

Category name in excel charts, using VBA without using any Cells

excelvbaexcel-vbaexcel-charts

提问by anonimou

I'm wanting to do a chart where the ticks on the X axis are not numbers but texts. For the purpose of learning: monthes like "January, August, February, November" or "Apples, Bananas, Cars".

我想做一个图表,其中 X 轴上的刻度不是数字而是文本。以学习为目的:像“一月、八月、二月、十一月”或“苹果、香蕉、汽车”这样的月份。

The trick is that I don't want these informations coming from any cells. It will be coded on Excel VBA.

诀窍是我不希望这些信息来自任何细胞。它将在 Excel VBA 上编码。

Maybe it is very very easy. I just don't know HOW look for it. I've beeing trying for the last couple hours, but still nothing. Look that it isn't the label of the axis that I'm wanting to change, but every data pointed in the Y-axis will have it name.

也许这很容易。我只是不知道如何寻找它。过去几个小时我一直在努力,但仍然一无所获。看起来它不是我想要更改的轴的标签,但是 Y 轴中指向的每个数据都会有它的名称。

Thanks in advance.

提前致谢。

采纳答案by hnk

You can directly fill data into the chart using VBA without using cells

您可以使用 VBA 直接将数据填充到图表中,而无需使用单元格

For e.g.

例如

ActiveChart.SeriesCollection(1).XValues = "={4,5,6}"

would change the category data displayed to 4,5,6. You can generate any string you require programmatically and change it.

会将显示的类别数据更改为 4、5、6。您可以以编程方式生成所需的任何字符串并进行更改。

回答by Portland Runner

Starting with data that looks like this:

从看起来像这样的数据开始:

enter image description here

在此处输入图片说明

You can set the axis labels using this line:

您可以使用此行设置轴标签:

ActiveChart.SeriesCollection(1).XValues = "={""Jan"",""Feb"",""Mar"",""Apr"",""May"",""Jun""}"


Full Example code:

完整示例代码:

Sub AddChart()
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlBarClustered
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).Values = "=Sheet1!$A:$A"
    ActiveChart.SeriesCollection(1).XValues = "={""Jan"",""Feb"",""Mar"",""Apr"",""May"",""Jun""}"
End Sub


Results:

结果:

enter image description here

在此处输入图片说明