473,503 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need to add more than 3 worksheets to excel workbook??

I was able to create three worksheets in my workbook, but when I go to
add the 4th I get an Invalid Index error. I must be leaving something
out to when adding 4 or more sheets. Thanks

Dim oExcel As Object
Dim oBook As Object
Dim oSheet1500 As Object
Dim oSheetARPPD As Object
Dim oSheetDFDDNA As Object
Dim oSheetDNE As Object

'Start a new workbook in Excel.
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet1500 = oBook.Worksheets(1)
oSheetARPPD = oBook.Worksheets(2)
oSheetDFDDNA = oBook.Worksheets(3)
oSheetDNE = oBook.Worksheets(4)

'Name teh sheets
oSheet1500.name = "1500"
oSheetARPPD.name = "AR-PPD"
oSheetDFDDNA.name = "DFD-DNA"
oSheetDNE.name = "DNE"
'oSheetEOB.name = "EOB"

Mar 14 '07 #1
3 13033
You code doesn't insert any new sheets, it just adds a new workbook to excel
and since workbooks come with 3 sheets already in them, you are only
erroring out when you attempt to refer to sheet 4.

For example, all that this code does is make a variable point to the already
existing sheet 1 in the workbook, not create a new sheet:

oSheet1500 = oBook.Worksheets(1)

You need to declare an object that represents the workbook's worksheets
collection. Then, once you have that you can call the .add method of the
worksheets collection and add new sheets to the collection similarly to how
you added a workbook to the workbooks collection.


"mike11d11" <mi*******@yahoo.comwrote in message
news:11*********************@n59g2000hsh.googlegro ups.com...
>I was able to create three worksheets in my workbook, but when I go to
add the 4th I get an Invalid Index error. I must be leaving something
out to when adding 4 or more sheets. Thanks

Dim oExcel As Object
Dim oBook As Object
Dim oSheet1500 As Object
Dim oSheetARPPD As Object
Dim oSheetDFDDNA As Object
Dim oSheetDNE As Object

'Start a new workbook in Excel.
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet1500 = oBook.Worksheets(1)
oSheetARPPD = oBook.Worksheets(2)
oSheetDFDDNA = oBook.Worksheets(3)
oSheetDNE = oBook.Worksheets(4)

'Name teh sheets
oSheet1500.name = "1500"
oSheetARPPD.name = "AR-PPD"
oSheetDFDDNA.name = "DFD-DNA"
oSheetDNE.name = "DNE"
'oSheetEOB.name = "EOB"

Mar 14 '07 #2
Could you get me started on how to create the collection of worksheets
and then add a fourth sheet. My excel workbook will in the end have
around 20 individual sheets. Sorry I'm farely new to the programming
world and any help would be greatly appreaciated. thanks

Mar 15 '07 #3
You'll need to make a COM reference to the MS Excel Object Library first.

Sub makeExcelSheet()
Dim xl As New Excel.ApplicationClass
Dim wb As New Excel.WorkbookClass

Dim i As Short
For i = 1 To 4
Dim ws As New Excel.WorksheetClass
wb.Worksheets.Add(ws)
Next
xl.workbooks.add(wb)
End Sub

But I just have to ask....

You are using VB.NET and not VB 6.0, right? This is a newsgroup for VB.NET
questions. Your code that you showed though, looks a little VB 6.0-ish to
me (we don't use "createObject" in .NET).


"mike11d11" <mi*******@yahoo.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
Could you get me started on how to create the collection of worksheets
and then add a fourth sheet. My excel workbook will in the end have
around 20 individual sheets. Sorry I'm farely new to the programming
world and any help would be greatly appreaciated. thanks

Mar 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8029
by: Iris | last post by:
I have 8 text files (tab delimited) that I would like to import into an Excel workbook as 8 individual worksheets but I cannot find any example code on this subject. Can anyone help me please???? ...
0
1885
by: dleh | last post by:
I have been tasked with producing a server application that will convert excel files to html. I have no experience of developing under Windows, (but plenty of UNIX/C++ experience) but I picked...
4
8695
by: paul.chae | last post by:
I have a table in Access with about 3000 records. There are ~60 unique values in the ID field for the 3000 records. What I would like to do is automatically generate multiple Excel worksheets...
1
3632
by: J Daniel Melton | last post by:
Hello, I am using late binding in a managed VC++ .NET 2003 application. I used KB 302902 (for C#) as a starting point and converted it to managed C++. I built a managed class that is intantiated...
4
14535
by: e.h.doxtator | last post by:
All I'm a Python newbie, and I'm just getting to the wonders of COM programming. I am trying to programmatically do the following: 1. Activate Excel 2. Add a Workbook 3. Add a Worksheet...
9
3923
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
0
1078
by: pdettorre | last post by:
Info: original Env: VS2005 with Office 2003 using Microsoft.Office.Interop.Excel (11.0) Have upgraded to VS2008 with office 2007 still using library Microsoft.Office.Interop.Excel (11.0) Ok,...
1
4843
by: MarkDotNet | last post by:
Hi I am trying to switch Excel worksheets in VBA. I get an error saying "subscript out of range". Please Help- Here is code (Fails on last 2 lines- Note that I ommited the recordset portion of...
7
2861
by: owuraku | last post by:
At the end of each year (i.e. on every December 31) I want to create a new Excel Workbook (spreadsheet). Thus on December 31, 2008, I want to create a workbook called 2009. Now at the day 1of each...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.