以编程方式访问图表源数据名称

我从一个相同的工作簿中复制了一些图表,但每个图表(42个图表,每个图表有6个系列)的源数据仍包含完整的文件名path。 源表格和单元格是相同的,所以我只想findpathstring,并用“”replace它。 但是,我找不到一个方法来获取sourcedata的名称(因为它出现在refedit框中)。 从那里,我可以取代我需要的东西。

我们拥有的是这样的:

ActiveChart.SeriesCollection(1).Values = "='C:\[oldfile.xls]Charts.Data'!R1C17:R1C28"

我只想把string中的部分作为string,并执行我的function来删除文件path。 如果我尝试从它得到一个string,即:

sourcestring = ActiveChart.SeriesCollection(1).Values

我得到一个错误; 看起来VBA从它读取时认为它是一个数组,但可以在分配给它时使用一个string。 有任何想法吗?

你是对的,你不能真正的出来在refedit框中显示的相同的公式…你必须操纵你正在使用的系列的.Formula或.FormulaR1C1属性,并重build公式和设置即值和/或xvalues属性。

这个代码应该可以工作,有几个函数可以用来提取公式的不同部分…我认为它应该适用于你,或者至less希望能帮助你找出最好的办法。

 Sub ChangeActiveChartData() ChangeChartData ActiveChart End Sub Sub ChangeChartData(TheChart As Chart) If TheChart Is Nothing Then Exit Sub Dim TheSeries As Series Set TheSeries = TheChart.SeriesCollection(1) Dim TheForm As String TheForm = TheSeries.FormulaR1C1 Dim XValsForm As String XValsForm = GetXValuesFromFormula(TheForm) Debug.Print XValsForm XValsForm = GetRangeFormulaFromFormula(XValsForm) Debug.Print XValsForm Dim ValsForm As String ValsForm = GetValuesFromFormula(TheForm) Debug.Print ValsForm ValsForm = GetRangeFormulaFromFormula(ValsForm) Debug.Print ValsForm XValsForm = "=" & TheChart.Parent.Parent.Name & "!" & XValsForm ' TheChart's parents parent is the worksheet; we're assuming the chart is embedded in a worksheet ValsForm = "=" & TheChart.Parent.Parent.Name & "!" & ValsForm TheSeries.XValues = XValsForm TheSeries.Values = ValsForm End Sub Function GetXValuesFromFormula(SeriesFormula As String) As String ' Find string between first and second commas Dim FormulaParts() As String FormulaParts = Split(SeriesFormula, ",") GetXValuesFromFormula = FormulaParts(1) End Function Function GetValuesFromFormula(SeriesFormula As String) As String ' Find string between second and third commas Dim FormulaParts() As String FormulaParts = Split(SeriesFormula, ",") GetValuesFromFormula = FormulaParts(2) End Function Function GetRangeFormulaFromFormula(TheFormula As String) As String ' return to the right of the ! character in theformula Dim ExclamPos As Integer ExclamPos = InStrRev(TheFormula, "!") If ExclamPos > 0 Then GetRangeFormulaFromFormula = Right(TheFormula, Len(TheFormula) - ExclamPos) Else GetRangeFormulaFromFormula = TheFormula End If End Function