tags:

views:

343

answers:

1

I have some charts I've copied over from an identical workbook, but the source data for each chart (42 charts, with 6 series in each) still contains the full filename path. The source sheet and cells are identical, so I just want to find the path string and replace it with "". However, I can't find a way to get the name of the sourcedata (as it appears in the refedit box). From there, I can replace what I need to.

What we have is this:

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

And I just want to get the part in "" as a string, and perform my function to remove the filepath. If I try and get a string from it, ie:

sourcestring = ActiveChart.SeriesCollection(1).Values

I get an error; it seems VBA considers it an array when reading from it, but can use a string when assigning to it. Any ideas?

+1  A: 

You're right, you can't really get out the same formula that's displayed in the refedit box...you have to manipulate the .Formula or .FormulaR1C1 property on the series you're working with, and rebuild the formula and set that to the values and/or xvalues property.

This code should work, there are a few functions for pulling out different parts of the formula...I think it should work for you, or at least hopefully help you figure out what's best to do...

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
Jon Fournier