Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Copy sheet into a new workbook

Hi,

I would like to copy a sample sheet from one workbook into a new workbook.
This should happen repeatidly (within for ... next statement), as the copied
sheet will be filled with data. So I wrote following macro:

Sub CopySheet()
Dim NewWb As Workbook
Dim NewWs As Worksheet

Set NewWb = Workbooks.Add

For i = 1 To 5
SheetTemplate. Copy NewWb
Set NewWs = ActiveSheet

'...Code
Next i
End Sub

.... which doesn't work. I get the 1004 run-time error (copy failed).

Can anybody help me to fix it?

Thank you!

Mike

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Copy sheet into a new workbook

Sub CopySheet()
Dim NewWb As Workbook
Dim NewWs As Worksheet

Set NewWb = Workbooks.Add

For i = 1 To 5
with NewWb
SheetTemplate.Copy After:=.Worksheets(.Worksheets.count)
end with
Set NewWs = ActiveSheet

'...Code
Next i
End Sub

SheetTemplate is troubling. I don't know what it is, where it is, or how it
is defined, so if you have a problem it probably will be with that.

--
Regards,
Tom Ogilvy


"Mike" wrote:

Hi,

I would like to copy a sample sheet from one workbook into a new workbook.
This should happen repeatidly (within for ... next statement), as the copied
sheet will be filled with data. So I wrote following macro:

Sub CopySheet()
Dim NewWb As Workbook
Dim NewWs As Worksheet

Set NewWb = Workbooks.Add

For i = 1 To 5
SheetTemplate. Copy NewWb
Set NewWs = ActiveSheet

'...Code
Next i
End Sub

... which doesn't work. I get the 1004 run-time error (copy failed).

Can anybody help me to fix it?

Thank you!

Mike

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Copy sheet into a new workbook

Thank you Tom!

I already found the problem. I didn't use the destination in the copy
statement (After:=), so it must be exactly as you suggested.

SheetTemplate is the CodeName of the sheet that is to be copied, I prefer to
use it rather than reference throug the Sheets() collection.

Regards,

Mike

"Tom Ogilvy" wrote:

Sub CopySheet()
Dim NewWb As Workbook
Dim NewWs As Worksheet

Set NewWb = Workbooks.Add

For i = 1 To 5
with NewWb
SheetTemplate.Copy After:=.Worksheets(.Worksheets.count)
end with
Set NewWs = ActiveSheet

'...Code
Next i
End Sub

SheetTemplate is troubling. I don't know what it is, where it is, or how it
is defined, so if you have a problem it probably will be with that.

--
Regards,
Tom Ogilvy


"Mike" wrote:

Hi,

I would like to copy a sample sheet from one workbook into a new workbook.
This should happen repeatidly (within for ... next statement), as the copied
sheet will be filled with data. So I wrote following macro:

Sub CopySheet()
Dim NewWb As Workbook
Dim NewWs As Worksheet

Set NewWb = Workbooks.Add

For i = 1 To 5
SheetTemplate. Copy NewWb
Set NewWs = ActiveSheet

'...Code
Next i
End Sub

... which doesn't work. I get the 1004 run-time error (copy failed).

Can anybody help me to fix it?

Thank you!

Mike

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 sheet cells into differnt workbook/sheet, How? IVLUTA Excel Discussion (Misc queries) 2 June 2nd 09 11:16 PM
Copy Sheet from One workbook to another EXISTING workbook Ken Soenen Excel Programming 0 February 12th 06 04:07 PM
automatic copy and paste from sheet to sheet in a workbook ramseyjramseyj Excel Programming 6 December 11th 04 12:37 AM
Copy sheet from one workbook to another workbook problem Ron de Bruin Excel Programming 3 August 5th 04 07:19 PM


All times are GMT +1. The time now is 07:44 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"