Add new Worksheet at the lst in Excel VSTO C#


I am working on building an excel Add-In for a few back-end operations. In a few scenarios, I need to create new worksheets dynamically and populate data and chart reports. I am able to add a new worksheet. But the requirement is, The new worksheet should 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 beginning.

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 parameters.

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


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

The code snipped to add a new worksheet at first in the 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: