在VBA中更改ChartObject的x轴 [英] Changing x-axis of a ChartObject in VBA
问题描述
如果我想将x轴更改为 G5范围内的数据:G105
超过图2
然后我将其放入我的VBA子程序中:
If I want to change my x-axis to the data inside the range G5:G105
over Chart 2
then I put this into my VBA subroutine:
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.SeriesCollection(1).XValues = "='Q1'!$G$5:$G$105"
如何做到这一点,我可以有固定的$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $我试图使用
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
ActiveChart.SeriesCollection(1).XValues = "='Q1'!Range("G5").Resize(I, 1)"
其中 I
是在子例程的前面部分定义的一些整数
变量。
where I
is some Integer
variable defined in a preceding part of the subroutine.
但是它不工作
推荐答案
我害怕你的范围='Q1'!范围(G5) .Resize(I,1)
是字符串和代码的非法组合。尝试此范围: Range(Q1!G5)。调整大小(i,1)
。
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)
.
例如:
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
这篇关于在VBA中更改ChartObject的x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!