Copy worksheet not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepadevid
    New Member
    • Sep 2008
    • 1

    #1

    Copy worksheet not working

    Hi,

    when I tried to copy a worksheet programmaticall y in Excel 2007 the chartObjects are not getting copied. I am getting empty charts inside my new worksheet. This is working in Excel 2003.

    Excel.Applicati on.Worksheets(" Sheet1").ChartO bjects.Count //I am getting 2 here.

    Excel.Applicati on.worksheets(" Sheet1").Copy After:= Excel.Applicati on.Worksheets(" Sheet2")

    Excel.Applicati on.Worksheets(" Sheet1 (2)").ChartObje cts.Count // I am getting zero here.

    Please advise me.

    Thanks
  • janders468
    Recognized Expert New Member
    • Mar 2008
    • 112

    #2
    I tested this in xl 2003 and assume it is the same. You need to invoke the copy method of the chartobjects, not the sheet itself. Such as:

    Code:
    Excel.Worksheets("Sheet1").ChartObjects.Copy
    Excel.Worksheets("Sheet2").Paste
    Might be a better way but that will copy the chart object

    Comment

    • Puffy
      New Member
      • Aug 2010
      • 2

      #3
      Can't copy Charts (pivot) from one worksheet to another in Excel 2007

      I have the same issue with C#. I need to copy the contents of one worksheet (Excel.Workshee t) to another worksheet. Everything gets copied (including pivot charts) when I execute the code for the 1st time. But when I execute the code again, the charts (pivot charts) in the source worksheet dont get copied to the destination worksheet in Excel 2007. This works fine in Excel 2003.
      I'm using .NET 2.0!
      can you please help me on this...

      this is the code I'm using;
      Code:
              public static void copy(Excel.Worksheet src, Excel.Worksheet dest)
              {
                  Excel.Range range;
                  Excel.Range range2;
                  
                  range = src.get_Range(src.Cells[1, 1], src.Cells[src.Rows.Count, src.Columns.Count]); 
                  range2 = dest.get_Range(dest.Cells[1, 1], dest.Cells[1, 1]);
                  range.Copy(range2);
              }
      Last edited by Puffy; Aug 10 '10, 05:56 AM. Reason: added more info....

      Comment

      Working...