我在Excel / PowerPoint中获得了图表。在此图表中,我想从
获取每个数据点的值的源单元地址 ActiveChart.SeriesCollection(i).Points(y)
因为Excel自动生成系列(这个图表不是用VBA制作的)所以我不能从头开始进行这种参考(不是自己定义数据系列)。 我知道如何使它成为一种相反的方式 - 如何从精确的单元格中生成SeriesCollection.Point,但我想在现成的图表上使用它,所以不要再在VBA中从头开始定义图表。 / p>
感谢您的帮助!
答案 0 :(得分:0)
这是放置在Excel中的活动表格中的图表的简单解决方案。代码中的注释中有一些假设和提示。
Sub GetSourceCellsAddress()
'define chart first...
Dim CH As Chart
Set CH = ActiveSheet.ChartObjects(1).Chart
'...and its first series (which is line type)
Dim SC As Series
Set SC = CH.SeriesCollection(1)
'what we need is a series formula where you find reference to the range
Dim strFormula As String
strFormula = SC.Formula
'test here:
Debug.Print strFormula 'result: =SERIES(Sheet1!$D$3,,Sheet1!$D$4:$D$8,1)
'but in fact we need the range with cell values, retrieve it from the formula
Dim strRangeFromFormula As String
strRangeFromFormula = Mid(strFormula, _
InStrRev(strFormula, "!") + 1, _
InStrRev(strFormula, ",") - InStrRev(strFormula, "!") - 1)
'test here
Debug.Print strRangeFromFormula 'result: $D$4:$D$8
'in fact our point PT is a range type, so, iterate through cells
Dim rngPT As Range
For Each rngPT In Range(strRangeFromFormula)
Debug.Print rngPT.Address 'results >>$D$4, $D$5, $D$6, $D$7, ....
Next rngPT
End Sub