ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Copy sheet into a new workbook (https://www.excelbanter.com/excel-programming/385337-copy-sheet-into-new-workbook.html)

Mike

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


Tom Ogilvy

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


Mike

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



All times are GMT +1. The time now is 10:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com