none
Copy worksheet after last sheet RRS feed

  • Question

  • Hi

    I want to copy a sheet from one workbook to another but want to make sure that the sheet I am copying is after the last sheet in the 'receiving'  workbook. I do not always know how many sheets there are in the receiving workbook.

    I tried copy after:=workbooks(filename).sheets(5)   but this assume that 5 is the last sheet (which it will not always be).

    Can anyone help please?

    thank you.

    Peter

     

    Tuesday, October 25, 2016 10:57 AM

Answers

  • Try

    after:=workbooks(filename).sheets(workbooks(filename).Sheets.count)


    Best Regards,
    Asadulla Javed,
    Jadavpore & Asansol

    • Marked as answer by py1 Tuesday, October 25, 2016 2:04 PM
    Tuesday, October 25, 2016 1:57 PM
    Answerer
  • Just another way to copy worksheet between workbooks and append the copy at the end of the 'receiving' workbook.

    using Spire.Xls; namespace CopySheet { class Program { static void Main(string[] args) { //Load source file Workbook sourceBook = new Workbook(); sourceBook.LoadFromFile(@"source.xlsx"); //Load destination file Workbook destBook = new Workbook(); destBook.LoadFromFile(@"destination.xlsx"); //Get the worksheet that you want to copy from source workbook Worksheet worksheet = sourceBook.Worksheets[1]; //Add the copy of to destination workbook destBook.Worksheets.AddCopy(worksheet); //Save changes to destionation destBook.SaveToFile("destination.xlsx"); } } }


    • Edited by Ashok Kumar_ Friday, October 28, 2016 8:02 AM
    • Marked as answer by py1 Monday, October 31, 2016 8:49 AM
    Friday, October 28, 2016 7:47 AM

All replies

  • Try

    after:=workbooks(filename).sheets(workbooks(filename).Sheets.count)


    Best Regards,
    Asadulla Javed,
    Jadavpore & Asansol

    • Marked as answer by py1 Tuesday, October 25, 2016 2:04 PM
    Tuesday, October 25, 2016 1:57 PM
    Answerer
  • Just another way to copy worksheet between workbooks and append the copy at the end of the 'receiving' workbook.

    using Spire.Xls; namespace CopySheet { class Program { static void Main(string[] args) { //Load source file Workbook sourceBook = new Workbook(); sourceBook.LoadFromFile(@"source.xlsx"); //Load destination file Workbook destBook = new Workbook(); destBook.LoadFromFile(@"destination.xlsx"); //Get the worksheet that you want to copy from source workbook Worksheet worksheet = sourceBook.Worksheets[1]; //Add the copy of to destination workbook destBook.Worksheets.AddCopy(worksheet); //Save changes to destionation destBook.SaveToFile("destination.xlsx"); } } }


    • Edited by Ashok Kumar_ Friday, October 28, 2016 8:02 AM
    • Marked as answer by py1 Monday, October 31, 2016 8:49 AM
    Friday, October 28, 2016 7:47 AM
  • thanks for this - I appreciate it.
    Monday, October 31, 2016 8:49 AM