VBA:修改图表数据范围 [英] VBA: Modify chart data range
问题描述
我的「图表资料范围」为 ='sheet1'!$ A $ 1:$ Z $ 10
。我想制作一个VBA宏(或者如果有人知道一个公式,我可以使用,但我不能一一出来)增加 chart1 $ c $的范围的结束列c>每次我运行宏1。基本上:
My "Chart data range" is ='sheet1'!$A$1:$Z$10
. I'd like to make a VBA macro (or if anybody knows a formula I can use, but I couldn't figure one out) to increase the ending column of the range for chart1
by 1 every time I run the macro. So essentially:
chart1.endCol = chart1.endCol + 1
使用 ActiveChart
的语法是什么?还是有更好的方法?
What is the syntax for this using ActiveChart
or is there a better way?
推荐答案
假设您想扩展范围(通过添加一个额外的列)为图中每个系列添加一个观察(而不是添加一个新系列),您可以使用以下代码:
Assuming that you want to expand the range (by adding one extra column) to add one more observation for each series in you diagram (and not to add a new series), you could use this code:
Sub ChangeChartRange()
Dim i As Integer, r As Integer, n As Integer, p1 As Integer, p2 As Integer, p3 As Integer
Dim rng As Range
Dim ax As Range
'Cycles through each series
For n = 1 To ActiveChart.SeriesCollection.Count Step 1
r = 0
'Finds the current range of the series and the axis
For i = 1 To Len(ActiveChart.SeriesCollection(n).Formula) Step 1
If Mid(ActiveChart.SeriesCollection(n).Formula, i, 1) = "," Then
r = r + 1
If r = 1 Then p1 = i + 1
If r = 2 Then p2 = i
If r = 3 Then p3 = i
End If
Next i
'Defines new range
Set rng = Range(Mid(ActiveChart.SeriesCollection(n).Formula, p2 + 1, p3 - p2 - 1))
Set rng = Range(rng, rng.Offset(0, 1))
'Sets new range for each series
ActiveChart.SeriesCollection(n).Values = rng
'Updates axis
Set ax = Range(Mid(ActiveChart.SeriesCollection(n).Formula, p1, p2 - p1))
Set ax = Range(ax, ax.Offset(0, 1))
ActiveChart.SeriesCollection(n).XValues = ax
Next n
End Sub
这篇关于VBA:修改图表数据范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!