Add new Worksheet at the lst in Excel VSTO C#


Excel Add in Applications using Visual Studio Tools for Office (VSTO) in C#. How to add new Excel Worksheet at last of all existing worksheets in a work book. Using Globals.ThisWorkbook.Worksheets.Add().

The resource has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this resource.
This resource will be reviewed shortly.
If you think this resource contain inappropriate content, please report to webmaster.
I am working on builidng excel Add-In for few back end operations. In few scenarios, I need to create new worksheets dynamically and populate data and chart reports.

I am able to add new worksheet. But the requirement is, The new worksheet shoud add the last of all existing worksheets.

The code snipped to add new worksheet


Globals.ThisWorkbook.Worksheets.Add()

The above code adding sheet in the begining.

There is overloaded Add() methiod with parametes
Globals.ThisWorkbook.Worksheets.Add(Before:. After:, Count:, Type)

It is not mandatory to provide default values for all paramets.

The code snipped to add new worksheet at last in active workbook


int worksheetsCount = Globals.ThisWorkbook.Worksheets.Count;
Globals.ThisWorkbook.Worksheets.Add(After: Globals.ThisWorkbook.Worksheets[worksheetsCount])

The code snipped to add new worksheet at first in active workbook


int worksheetsCount = Globals.ThisWorkbook.Worksheets.Count;
Globals.ThisWorkbook.Worksheets.Add(Before: Globals.ThisWorkbook.Worksheets[1])


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: