使用Excel VBA在图表中从源数据更改工作表名称

时间:2018-09-04 16:41:16

标签: excel-vba

我在Excel中的工作表上有一个图表。源数据位于另一张纸上。我想将Digram中的源数据引用重定向到另一个工作表,该工作表与第一个工作表相同,但实际数据除外。

如何从图中的源数据引用中获取当前的工作表名称,并将其替换为其他工作表的名称?我需要使用VBA完成此操作。

谢谢!

2 个答案:

答案 0 :(得分:0)

对不起,您的回复很晚。我在你们和一些Google的帮助下像这样解决了它。不是最干净的代码,但是它可以工作。谢谢!

Sub ReplaceSheetName(strNewSheetName As String)
    Dim srs As Series
    Dim strSourceDataSheetName As String
    Dim strTempFormula As String

    strNewSheetName = "'" & strNewSheetName & "'"
    For Each srs In ActiveSheet.ChartObjects(1).Chart.SeriesCollection
        If InStr(1, srs.Name, "Budget", vbTextCompare) > 0 Then
        Else
            strSourceDataSheetName = GetSheetNameFromChartSourceData(srs.Formula, "(", "!")
            strTempFormula = Replace(srs.Formula, strSourceDataSheetName, strNewSheetName, vbTextCompare)
            srs.Formula = WorksheetFunction.Substitute(srs.Formula, srs.Formula, strTempFormula)
        End If
    Next
    ActiveSheet.ChartObjects(1).Chart.Refresh
End Sub

Private Function GetSheetNameFromChartSourceData(strSourceDataFormula As String, strStartDelimiter As String, strEndDelimiter As String) As String
    Dim firstDelPos As Integer
    Dim secondDelPos As Integer
    Dim stringBwDels As String

    On Error GoTo ErrorHandler

    firstDelPos = InStr(strSourceDataFormula, strStartDelimiter) 'Position of start delimiter

    'If end delimiter is the same as the start delimiter they will be the same, hence no string is extracted.
    If strStartDelimiter = strEndDelimiter Then
        secondDelPos = InStrRev(strSourceDataFormula, strEndDelimiter) 'Position of end delimiter
    Else
        secondDelPos = InStr(strSourceDataFormula, strEndDelimiter) 'Position of end delimiter
    End If

    If firstDelPos = 0 Or secondDelPos = 0 Then
        stringBwDels = strSourceDataFormula
    Else
        stringBwDels = Mid(strSourceDataFormula, firstDelPos + 1, secondDelPos - firstDelPos - 1) 'Extract the string between two delimiters
    End If

    GetSheetNameFromChartSourceData = stringBwDels
    Exit Function
ErrorHandler:
    MsgBox "An error occurred while extracting substring between the given separators " & """" & strStartDelimiter & """" & " and " & """" & strEndDelimiter & """" & ". Error message: " & Err.Description, vbOKOnly, "Error"
    Resume Next
End Function

答案 1 :(得分:0)

这是一项很好的工作。我也遇到了同样的问题,但是我以较短的方式解决了它,利用现成的功能来实现相同的目的。下面是我的代码

Sub changeSheetRef()
'This code replaces the sheet name part of all data series in a plot _
 with the current sheet name

newSheetRef = ActiveSheet.Name

For i = 1 To ActiveSheet.ChartObjects.Count
    ActiveSheet.ChartObjects(i).Activate
        For j = 1 To ActiveChart.SeriesCollection().Count
            oldDataSource = ActiveChart.SeriesCollection(j).Formula
            oldSheetRef = Split(Split(oldDataSource, ",")(2), "'")(1)
            newDataSource = Replace(oldDataSource, oldSheetRef, newSheetRef)
            ActiveChart.SeriesCollection(j).Formula = newDataSource
        Next
    ActiveSheet.ChartObjects(i).Chart.Refresh
Next
MsgBox "All chart data series have been updated to match equivalent data on the current worksheet, courtesy of SaeedSoft Inc.", , "SaeedSoft Inc."
End Sub