在 VBA 中更改图表对象的 x 轴

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

Changing x-axis of a ChartObject in VBA

excelvbaexcel-vbacharts

提问by Olle Sj?gren

If I want to change my x-axis to the data inside the range G5:G105over Chart 2then I put this into my VBA subroutine:

如果我想将我的 x 轴更改为范围内的数据G5:G105Chart 2那么我将其放入我的 VBA 子例程中:

ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.SeriesCollection(1).XValues = "='Q1'!$G:$G5"

How do I make it such that I can have some arbitrary range INSTEAD OF the fixed $G$5:$G$105. I've tried to use

我如何使它可以有一些任意范围而不是固定的$G$5:$G$105. 我试过用

ActiveChart.SeriesCollection(1).XValues = "='Q1'!Range("G5").Resize(I, 1)"

where Iis some Integervariable defined in a preceding part of the subroutine.

其中IInteger子程序前面部分中定义的某个变量。

However it doesn't work.

但是它不起作用。

回答by Olle Sj?gren

I'm afraid your range "='Q1'!Range("G5").Resize(I, 1)"is an illegal mix of a strings and code. Try this range instead: Range("Q1!G5").Resize(i, 1).

恐怕您的范围"='Q1'!Range("G5").Resize(I, 1)"是字符串和代码的非法组合。试试这个范围:Range("Q1!G5").Resize(i, 1)

Full example:

完整示例:

Option Explicit

Sub ChartTest()

    Dim i As Integer

    i = 2

    ActiveSheet.ChartObjects("Chart 2").Activate
    ActiveChart.SeriesCollection(1).XValues = Range("Q1!G5").Resize(i, 1)

End Sub