Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 542
Default Copy Worksheets To New Workbook

From an existing workbook I am trying to create a new workbook and copy all
worksheets (with the excpetion of the worksheet named "code") from the
workbook where the code is executing to the new workbook.
I keep receiving run time error 1004, application defined or object defined
error.

The code creates a new workbook, i.e. book1 but the following line fails:
ws.Copy after:=Workbooks(wbreport.Name).Sheets(wbreport.Sh eets.Count)

Here is my code:
Private Sub cmdcopyworkbook_Click()

Dim wbcode As Workbook
Dim wbreport As Workbook

Dim ws As Worksheet

Set wbcode = ActiveWorkbook

Workbooks.Add

Set wbreport = Workbooks(Workbooks.Count)

For Each ws In wbcode.Worksheets

If ws.Name < "code" Then
ws.Copy after:=Workbooks(wbreport.Name).Sheets(wbreport.Sh eets.Count)
End If

Next ws

End Sub


Any help would be appreciated. Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Copy Worksheets To New Workbook

You are probably getting the error on this line:

ws.Copy after:=Workbooks(wbreport.Name).Sheets(wbreport.Sh eets.Count)

Change to:


ws.Copy after:=Workbooks(wbreport.Name).Sheets(Sheets.Coun t)




"James" wrote in message
...
From an existing workbook I am trying to create a new workbook and copy
all
worksheets (with the excpetion of the worksheet named "code") from the
workbook where the code is executing to the new workbook.
I keep receiving run time error 1004, application defined or object
defined
error.

The code creates a new workbook, i.e. book1 but the following line fails:
ws.Copy after:=Workbooks(wbreport.Name).Sheets(wbreport.Sh eets.Count)

Here is my code:
Private Sub cmdcopyworkbook_Click()

Dim wbcode As Workbook
Dim wbreport As Workbook

Dim ws As Worksheet

Set wbcode = ActiveWorkbook

Workbooks.Add

Set wbreport = Workbooks(Workbooks.Count)

For Each ws In wbcode.Worksheets

If ws.Name < "code" Then
ws.Copy after:=Workbooks(wbreport.Name).Sheets(wbreport.Sh eets.Count)
End If

Next ws

End Sub


Any help would be appreciated. Thanks.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Copy Worksheets To New Workbook

If wbreport is a workbook, then you can use:
ws.copy _
after:=wbreport.sheets(wbreport.sheets.count)

I didn't see anything that would cause the error, but maybe you could add a:

msgbox wbreport.name
before the .copy line. Maybe you're pointing to the wrong workbook?

Another way that doesn't rely on the position of the workbook:

Option Explicit
Private Sub cmdcopyworkbook_Click()

Dim wbCode As Workbook
Dim wbReport As Workbook
Dim ws As Worksheet

Set wbCode = ActiveWorkbook

Set wbReport = Workbooks.Add(1) 'single sheet in a new workbook

For Each ws In wbCode.Worksheets
If lcase(ws.Name) < lcase("code") Then
With wbReport
ws.Copy _
after:=.Sheets(.Sheets.Count)
End With
End If
Next ws

if wbreport.sheets.count = 1 then
'do nothing
else
'remove that inital sheet
application.displayalerts = false
wbreport.worksheets(1).delete 'remove that inital sheet
application.displayalerts = true
end if

End Sub

James wrote:

From an existing workbook I am trying to create a new workbook and copy all
worksheets (with the excpetion of the worksheet named "code") from the
workbook where the code is executing to the new workbook.
I keep receiving run time error 1004, application defined or object defined
error.

The code creates a new workbook, i.e. book1 but the following line fails:
ws.Copy after:=Workbooks(wbreport.Name).Sheets(wbreport.Sh eets.Count)

Here is my code:
Private Sub cmdcopyworkbook_Click()

Dim wbcode As Workbook
Dim wbreport As Workbook

Dim ws As Worksheet

Set wbcode = ActiveWorkbook

Workbooks.Add

Set wbreport = Workbooks(Workbooks.Count)

For Each ws In wbcode.Worksheets

If ws.Name < "code" Then
ws.Copy after:=Workbooks(wbreport.Name).Sheets(wbreport.Sh eets.Count)
End If

Next ws

End Sub

Any help would be appreciated. Thanks.


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy all worksheets to another workbook, excl. duplicate sheets already in other workbook ARbitOUR[_15_] Excel Programming 2 May 27th 09 06:14 AM
Copy worksheets to a different workbook Angel C[_2_] Excel Programming 5 February 13th 09 04:36 PM
Copy worksheets within a workbook robert morris Excel Discussion (Misc queries) 0 February 24th 08 01:25 PM
Copy Worksheets from one Workbook to Another halem2 Excel Worksheet Functions 3 March 25th 06 06:04 AM
Copy four worksheets from one workbook into a new workbook.e-mail Francis Brown Excel Programming 1 October 3rd 05 12:24 AM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"