PC Review


Reply
Thread Tools Rate Thread

Create a new workbook and add sheets

 
 
HSalim[MVP]
Guest
Posts: n/a
 
      31st Oct 2008
Hi,
I am parsing a text file into component parts:

I want to open a new workbook, add a few worksheets, ad data, save file.
I can add the workbook but I can't seem to add worksheets to it

How can I fix the code below?
Thanks
Habib

----------------------
SrcFile = GetFile()
XLFile = Left(srcFile, Len(srcFile) - 4) & ".xls"
Set wkbook = Workbooks.Add()

wkbook.Activate

wksheets = Array("Invoice", "Payment", "Reference", "Summary", "Other")
For i = 0 To UBound(wksheets)
Worksheets.Add(Before:=Worksheets(Worksheets.Count)).Name = "test"
ActiveSheet.Name = wksheets(i)
Next









 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      31st Oct 2008
I'd use:

Dim wkSheets As Variant
Dim i As Long
Dim wkBook As Workbook

Set wkBook = Workbooks.Add(1) 'single sheet
wkBook.Worksheets(1).Name = "deletemelater"

wkSheets = Array("Invoice", "Payment", "Reference", "Summary", "Other")
For i = LBound(wkSheets) To UBound(wkSheets)
With wkBook
.Worksheets.Add(Before:=.Worksheets(Worksheets.Count)).Name = wkSheets(i)
End With
Next i

Application.DisplayAlerts = False
wkBook.Worksheets("deletemelater").Delete
Application.DisplayAlerts = True



"HSalim[MVP]" wrote:
>
> Hi,
> I am parsing a text file into component parts:
>
> I want to open a new workbook, add a few worksheets, ad data, save file.
> I can add the workbook but I can't seem to add worksheets to it
>
> How can I fix the code below?
> Thanks
> Habib
>
> ----------------------
> SrcFile = GetFile()
> XLFile = Left(srcFile, Len(srcFile) - 4) & ".xls"
> Set wkbook = Workbooks.Add()
>
> wkbook.Activate
>
> wksheets = Array("Invoice", "Payment", "Reference", "Summary", "Other")
> For i = 0 To UBound(wksheets)
> Worksheets.Add(Before:=Worksheets(Worksheets.Count)).Name = "test"
> ActiveSheet.Name = wksheets(i)
> Next
>
>
>


--

Dave Peterson
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      31st Oct 2008
There's a typo in my code!!!

..Worksheets.Add(Before:=.Worksheets(Worksheets.Count)).Name = wkSheets(i)
should be:
..Worksheets.Add(Before:=.Worksheets(.Worksheets.Count)).Name = wkSheets(i)

Those leading dots mean that those items belong to the object in the previous
"With" statement.



Dave Peterson wrote:
>
> I'd use:
>
> Dim wkSheets As Variant
> Dim i As Long
> Dim wkBook As Workbook
>
> Set wkBook = Workbooks.Add(1) 'single sheet
> wkBook.Worksheets(1).Name = "deletemelater"
>
> wkSheets = Array("Invoice", "Payment", "Reference", "Summary", "Other")
> For i = LBound(wkSheets) To UBound(wkSheets)
> With wkBook
> .Worksheets.Add(Before:=.Worksheets(Worksheets.Count)).Name = wkSheets(i)
> End With
> Next i
>
> Application.DisplayAlerts = False
> wkBook.Worksheets("deletemelater").Delete
> Application.DisplayAlerts = True
>
> "HSalim[MVP]" wrote:
> >
> > Hi,
> > I am parsing a text file into component parts:
> >
> > I want to open a new workbook, add a few worksheets, ad data, save file.
> > I can add the workbook but I can't seem to add worksheets to it
> >
> > How can I fix the code below?
> > Thanks
> > Habib
> >
> > ----------------------
> > SrcFile = GetFile()
> > XLFile = Left(srcFile, Len(srcFile) - 4) & ".xls"
> > Set wkbook = Workbooks.Add()
> >
> > wkbook.Activate
> >
> > wksheets = Array("Invoice", "Payment", "Reference", "Summary", "Other")
> > For i = 0 To UBound(wksheets)
> > Worksheets.Add(Before:=Worksheets(Worksheets.Count)).Name = "test"
> > ActiveSheet.Name = wksheets(i)
> > Next
> >
> >
> >

>
> --
>
> Dave Peterson


--

Dave Peterson
 
Reply With Quote
 
HSalim[MVP]
Guest
Posts: n/a
 
      1st Nov 2008
Dave,
thanks for that. That did the trick.

While tinkering with the code, I found another way.

wkSheets = Array("Invoice", "Payment", "Reference", "Summary", "Other")

Application.SheetsInNewWorkbook = UBound(wkSheets) + 1
Set wkBook = Workbooks.Add
For i = LBound(wkSheets) To UBound(wkSheets)
With wkBook
.Sheets(i + 1).Name = wkSheets(i)
End With
Next i


Regards
Habib



"Dave Peterson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> There's a typo in my code!!!
>
> .Worksheets.Add(Before:=.Worksheets(Worksheets.Count)).Name = wkSheets(i)
> should be:
> .Worksheets.Add(Before:=.Worksheets(.Worksheets.Count)).Name = wkSheets(i)
>
> Those leading dots mean that those items belong to the object in the
> previous
> "With" statement.
>
>
>
> Dave Peterson wrote:
>>
>> I'd use:
>>
>> Dim wkSheets As Variant
>> Dim i As Long
>> Dim wkBook As Workbook
>>
>> Set wkBook = Workbooks.Add(1) 'single sheet
>> wkBook.Worksheets(1).Name = "deletemelater"
>>
>> wkSheets = Array("Invoice", "Payment", "Reference", "Summary", "Other")
>> For i = LBound(wkSheets) To UBound(wkSheets)
>> With wkBook
>> .Worksheets.Add(Before:=.Worksheets(Worksheets.Count)).Name =
>> wkSheets(i)
>> End With
>> Next i
>>
>> Application.DisplayAlerts = False
>> wkBook.Worksheets("deletemelater").Delete
>> Application.DisplayAlerts = True
>>
>> "HSalim[MVP]" wrote:
>> >
>> > Hi,
>> > I am parsing a text file into component parts:
>> >
>> > I want to open a new workbook, add a few worksheets, ad data, save
>> > file.
>> > I can add the workbook but I can't seem to add worksheets to it
>> >
>> > How can I fix the code below?
>> > Thanks
>> > Habib
>> >
>> > ----------------------
>> > SrcFile = GetFile()
>> > XLFile = Left(srcFile, Len(srcFile) - 4) & ".xls"
>> > Set wkbook = Workbooks.Add()
>> >
>> > wkbook.Activate
>> >
>> > wksheets = Array("Invoice", "Payment", "Reference", "Summary", "Other")
>> > For i = 0 To UBound(wksheets)
>> > Worksheets.Add(Before:=Worksheets(Worksheets.Count)).Name = "test"
>> > ActiveSheet.Name = wksheets(i)
>> > Next
>> >
>> >
>> >

>>
>> --
>>
>> Dave Peterson

>
> --
>
> Dave Peterson


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy two sheets from workbook & create new workbook Mike R. Microsoft Excel Programming 8 7th Aug 2009 07:09 PM
Multiple Sheets (Need to create 500 individual sheets in one workbook, pulling DATA Amaxwell Microsoft Excel Worksheet Functions 4 17th Aug 2006 06:23 AM
How to create workbook with multiple sheets control freak Microsoft Excel Misc 0 19th Jul 2006 06:54 PM
Create new workbook when 00 sheets are reached Corey Microsoft Excel Programming 1 22nd Jun 2006 07:38 AM
Create New Workbook - Name book - 4 Sheets - Name Sheets Greg Microsoft Excel Programming 6 12th Jun 2005 04:41 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:12 AM.