简体   繁体   中英

Maintaining a dynamic named range in a chart when copying a worksheet

I am trying to automate charts for a spreadsheet with a lot of worksheets.

I'm building all the charts/graphs I need on a template worksheet and making them dynamic using named ranges (OFFSET + COUNT) . Once I have this template completed I would like to be able to copy the worksheet (while keeping it in the same workbook) and have the charts update when I drop in new data on each new worksheet.

Each worksheet will use the same names for the ranges (generic financial words such as margin and volume ), so I've restricted them to the worksheet on which they're being used (rather than a global scope).

When I copy the worksheets, the named ranges referenced in the charts are replaced with the static cell addresses rather than copying with the chart. The dynamic named ranges copy with the worksheet and are only able to be referenced on the new worksheet (which is what I want).

Is there a way to make it so charts maintain the dynamic named ranges?

You can simply re-point the series values. This is a very simple case with 1 series collection and 1 chart where you copy sheet 1. There is a dynamic series called DynRange which already exists in sheet 1. The sub below simply sets the series in the copied chart back to this range.

You could develop this to loop over all the charts in the copied sheet. You might need to have already looped the original charts and all their series to store (in an array?) the chart names, series names/numbers, and associated named ranges so you can apply correctly to the new range.

Or loop and set chart 1 on sheet2, series 1 = chart 1 on sheet2 series 1 etc.

Note: You can save a worksheet as an official Excel template and use that.

Option Explicit

    Sub ResetRange()

        Sheets("Sheet1").Select
        Sheets("Sheet1").Copy Before:=Sheets(1)
        ActiveSheet.ChartObjects("Chart 1").Activate
        ActiveChart.FullSeriesCollection(1).Values = "=Sheet1!DynRange"

    End Sub

Main code:

And here is a rough and ready version, of what I mentioned, to loop over all the charts and all the series setting to the equivalent dynamic ranges in sheet 1. Note I only tested with 1 chart and 2 dynamic series.

Option Explicit

Public Sub ResetRange()

    Dim wb As Workbook
    Dim sourceSheet As Worksheet

    Set wb = ThisWorkbook
    Set sourceSheet = wb.Sheets("Sheet1")
    sourceSheet.Copy Before:=Sheets(1)

    Dim currChart As Long
    Dim currSeries As Series
    Dim thisChart As Chart
    Dim thisSeries As Long

    With ActiveSheet

        For currChart = 1 To .ChartObjects.Count

            Set thisChart = .ChartObjects(currChart).Chart

            For thisSeries = 1 To thisChart.SeriesCollection.Count

                thisChart.SeriesCollection(thisSeries).Formula = sourceSheet.ChartObjects(currChart).Chart.SeriesCollection(thisSeries).Formula

            Next thisSeries

            Set thisChart = Nothing

        Next currChart

    End With

    LoopNamedRanges ActiveSheet

End Sub

Private Sub LoopNamedRanges(ByVal ActiveSheet As Worksheet)

    Dim nm As Name

    For Each nm In ActiveWorkbook.Names

        If nm.RefersToRange.Parent.Name = ActiveSheet.Name Then

            nm.Delete

        End If

    Next nm

End Sub

Data:

代码运行

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM