views:

554

answers:

3

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?

A: 

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.

nate
Oh that's clever! I'd still like to split up the data (to meet the spec), but if there's no way around it, this is exactly the direction I'll take this. Thank-you!
Jeffrey
A: 

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

Use SetSourceData method

ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A3:D7")
DJ
Edit to fix erroneous copy comment
DJ
+1  A: 

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"))
Mark
EXACTLY what I was looking for. Thank-you.
Jeffrey
Wow, even before I gave the interop version?
Mark
Ended up doing a select on the entire workbook - but that array trick will come in handy down the road.
Jeffrey