4

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?

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Peter
  • 103
  • 9
  • The new sheets appears to have dynamic ranges associated with the sheet you are now in. Did you want them to refer to the original sheet? – QHarr Mar 16 '18 at 12:47
  • No I want them to refer to the new sheet. If I can get this to work I would be pasting new data into the table on the new sheet that could have a different length (hence the dynamic named ranges) and I'd like the chart to reference the data on the same sheet on which it's located. – Peter Mar 16 '18 at 15:14
  • I'm confused. Sorry. It looks, from the image, like they are pointing to the copied sheet (i.e. the new sheet) – QHarr Mar 16 '18 at 15:15
  • Yes, they are pointing to the copied sheet. The issue is that the chart dropped the named ranges when the worksheet was copied. If you look at the highlight in the function bar in the second image the chart references the static range rather than maintaining the named ranges as in the function bar in the first image. – Peter Mar 16 '18 at 15:27

1 Answers1

1

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:

Code run

References:

QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Thank you for putting together the code. When I ran the macro an error message came up and the debugger highlighted the "If nm.RefersToRange.Parent.Name = ActiveSheet.Name Then" line in the LoopNameRange section. Can you think of any reasons this would be causing issues? – Peter Mar 16 '18 at 17:44
  • Run-time error '1004': Application-defined or object defined error – Peter Mar 17 '18 at 12:11
  • if when you hit that error you open the immediate window (ctrl + G I think) and type ?nm.RefersToRange.Parent.Name hit enter what do you get? And also ?ActiveSheet.Name what do you get? – QHarr Mar 17 '18 at 12:14
  • For `?nm.RefersToRange.Parent.Name` I get an error message that says "Run-time error '1004': Application-defined or object-defined error". For `?ActiveSheet.Name` I get "Sheet1 (2)". – Peter Mar 17 '18 at 13:05
  • Ok. I am out at present but will get back to you – QHarr Mar 17 '18 at 13:24
  • What happens if you comment out LoopNamedRanges ActiveSheet ? – QHarr Mar 17 '18 at 16:40
  • The new sheet is created and there is no error message but the chart is referencing specific cells instead of the named ranges. – Peter Mar 17 '18 at 17:03
  • odd...as you can see in my gif it works. I am not sure I can debug further without a file :-( – QHarr Mar 17 '18 at 17:04
  • It seems like the easiest option is to just use the Excel template workaround as the charts will stay linked to the named ranges. Thanks for the help. – Peter Mar 17 '18 at 17:38
  • After trying to use the template method I seem to have run into another issue. When I update the worksheet name some of the charts are still referencing the old worksheet name instead of changing the dynamic named range reference to the new worksheet name. When I paste in new data the charts don't update because they are still referencing the data from the old worksheet name (which now shouldn't exist). Any idea why this might be happening? – Peter Mar 17 '18 at 18:58
  • I haven't explored this but may give it a go tomorrow. Otherwise could make for a good second question though be sure to show what you have tried and explain what isn't working with what you have tried. – QHarr Mar 17 '18 at 20:34