我正在尝试更新图表中的sourcedata。我不想要动态范围,因为每次点击按钮,图表都会从新范围拉出。如果我使用动态或命名范围,我将被锁定在该范围内。
我试过了:
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("A1:C3").Offset(0,3)
但这仅适用一次。为了清楚起见,我有一个按钮来更新电子表格中的图表。单击该按钮时,宏将查看活动图表中的当前数据并将源移动到3列。每次单击按钮并且数据必须保留在工作表上时,都会发生这种情况。我已经用尽了所有我知道的东西,可以在网上找到Goracle搜索。
任何帮助?
提前致谢, 莉莉丝
答案 0 :(得分:1)
SeriesCollection(j)
的{{1}}始终具有ActiveChart
属性,如下所示:
Formula
因此,您可以向项目添加一个自定义函数,该函数通过=SERIES(,,Sheet1!$A$1:$C$3,1)
解析字符串Split
并返回3列的相应范围转移:
$
然后通过每次重置公式进行转换:
Function ShiftBy3(ByVal formulaString As String) As String
cRange = Split(formulaString, "$")(1) + Split(formulaString, "$")(2) + Split(formulaString, "$")(3) + Split(Split(formulaString, "$")(4), ",")(0)
newAddress = Range(cRange).Offset(0,3).Address
ShiftBy3 = Split(formulaString, "$")(0) + newAddress + "," + Split(Split(formulaString, "$")(4), ",")(1)
End Function
答案 1 :(得分:1)
一种方法是更新图表上每个系列的Formula
属性:
Sub Tester()
Const SER As String = "=SERIES("
Dim s As Series, f As String, arr, rng1, rng2, sn
Dim cht As Chart
Set cht = ActiveSheet.ChartObjects("Chart 2").Chart
sn = ActiveSheet.Name
For Each s In cht.SeriesCollection
f = s.Formula
Debug.Print f '<<<added
f = Replace(f, SER, "")
f = Left(f, Len(f) - 1)
arr = Split(f, ",")
Set rng1 = Range(arr(1))
Set rng2 = Range(arr(2))
s.Formula = SER & Join(Array(arr(0), _
rng1.Parent.Name & "!" & rng1.Offset(0, 3).Address(), _
rng2.Parent.Name & "!" & rng2.Offset(0, 3).Address(), _
arr(3)), ",") & ")"
Next s
End Sub
答案 2 :(得分:0)
这是我老板提出的工作代码。
Dim var
var = Cells(4, 4)
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range(Cells(1, 1 + var), Cells(3, 3 + var))
Cells(4, 4).Value = Cells(4, 4).Value + 3
感谢Tim和Matteo为您提供所有时间和帮助。我真的很感激它!