Question

I've created a two-worksheet template in Excel - the first sheet is for pretty charts, and the other sheet is for the data that drives those charts. I've written a vb.net 2005 app that can dump in all my data on the second worksheet, and the chart worksheet updates perfectly.

I would like to do this report several times over in the same workbook. (So the tabs would read 'Person1 - Chart', 'Person1 - Data', 'Person2 - Chart', 'Person2 - Data', etc.)

My solution was to, for every person this report was going to be run for, copy the chart template, and then copy the data template. The problem is that every chart template that is created points to the original data worksheet. How can I set what worksheet each chart worksheet is pointing at?

Is there a way to copy worksheets in pairs, maintaining the relationship to themselves, rather than to the parent?

Was it helpful?

Solution

Yes, select both sheets before you use the copy function. If you're doing it manually, don't forget to deselect afterwards, or you'll be entering data into both.

In VBA:

Sheets(Array("Chart template", "Data template")).Select
ActiveWindow.SelectedSheets.Copy After:=Sheets("Data template")

In Interop:

App.ActiveWorkbook.Sheets(New String()
        {"Chart template", "Data template"}).Select()
App.ActiveWindow.SelectedSheets.Copy(After:=
        App.ActiveWorkbook.Sheets("Data template"))

OTHER TIPS

I haven't used a programmed app to make this work, but if you bring the data to the same sheet and set up the sheet using the Page Break Preview to avoid printing the data sheet, then you can copy and paste the sheet over and over and your chart references will stay valid.

Depending on the amount of data you're using this may not be an ideal solution, but its how I've managed this problem.

You can fix up the references in the Chart after you copy.

Use SetSourceData method

ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A3:D7")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top