Solved

VB.Net - Excel Adding Worksheet Causing Error

Posted on 2013-12-13
3
506 Views
Last Modified: 2013-12-13
Good Day Experts!

I have a very odd happening in my little program here. Currently I am making 3 tabs for my Excel output.  I am trying to add a fourth one and it says you are outta luck...actually "invalid Index".  

Here is how I am doing it:

Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oSheet2 As Excel.Worksheet
Dim oSheet3 As Excel.Worksheet
Dim oSheet4 As Excel.Worksheet

oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet
oSheet.Name = "Velocity"
oSheet2 = oWB.Worksheets(2)
oSheet2.Name = "Paradox"
oSheet3 = oWB.Worksheets(3)
oSheet3.Name = "Total"
oSheet4 = oWB.Worksheets(4)
oSheet4.Name = "5 Velocity Simplified"

It has started erroring when I added the above 2 lines for oSheet4.  When I comment those 2 lines it will not error!!!

Is there some limit or am I doing it wrong you think?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
3 Comments
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39716785
Hi

3 Sheets is the initial default

First add another sheet

oWB.Sheets.Add After:=oWB.Sheets(oWB.Sheets.Count)

Open in new window

Or use before adding wbk
oXL.SheetsInNewWorkbook = 4

Open in new window

Regards
0
 

Author Closing Comment

by:Jimbo99999
ID: 39716825
That is so cool...I did not know that! Another excellent tip for the knowledge base.

Thanks,
jimbo99999
0
 
LVL 40
ID: 39718009
Be careful. 3 sheets is the default, but it can be changed in Excel Options, so you cannot always count on that value. On my system, the initial count is 1.

You should check oWB.Sheets.Count first, and then act accordingly. You might need to add more than one sheet is the initial count is less than 3, or remove extra sheets if the initial count is more than 4 and you do not want more than 4 sheets in the Workbook.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question