Domanda

Ho alcuni grafici che ho copiato da una cartella di lavoro identica, ma i dati di origine per ciascun grafico (42 grafici, con 6 serie in ciascuno) contengono ancora il percorso completo del nome file. Il foglio di origine e le celle sono identici, quindi voglio solo trovare la stringa del percorso e sostituirla con " " ;. Tuttavia, non riesco a trovare un modo per ottenere il nome del sourcedata (come appare nella casella refedit). Da lì, posso sostituire ciò di cui ho bisogno.

Quello che abbiamo è questo:

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

E voglio solo ottenere la parte in " " come stringa ed eseguo la mia funzione per rimuovere il percorso del file. Se provo a ricavarne una stringa, ovvero:

sourcestring = ActiveChart.SeriesCollection (1) .Values ??

Ricevo un errore; sembra che VBA lo consideri un array quando legge da esso, ma può usare una stringa durante l'assegnazione ad esso. Qualche idea?

È stato utile?

Soluzione

Hai ragione, non puoi davvero ottenere la stessa formula visualizzata nella casella refedit ... devi manipolare la proprietà .Formula o .FormulaR1C1 sulla serie con cui stai lavorando e ricostruire il formula e impostarlo sulla proprietà valori e / o xvalues.

Questo codice dovrebbe funzionare, ci sono alcune funzioni per estrarre diverse parti della formula ... Penso che dovrebbe funzionare per te, o almeno speriamo che ti aiuti a capire cosa è meglio fare ...

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top