Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy method of Worksheet class failed

Status
Not open for further replies.

smelba

Technical User
Jan 8, 2003
7
US
I have an Excel workbook with VBA code that makes several copies of one of the sheets. Every time I run the macro, I get the following error:

Run-time error '1004':
Copy method of Worksheet class failed

The error always occurs when there are 58 sheets in the workbook and the macro is attempting to create the 59th. My code is as follows:

For i = 1 To {variable} Step 1
Sheets({sheetname}).Copy After:=Sheets(Sheets.Count)
Next i

I am running Excel 2000 and VB 6.0 in Win 2000.

Any help will be greatly appreciated!
 
You're going to have to post more of your code. I just ran the macro below twice and now have 141 sheets in my workbook with no problem.
[blue]
Code:
Option Explicit
Sub test()
Dim i As Integer

  For i = 1 To 70 Step 1
    Sheets("Sheet1").Copy After:=Sheets(Sheets.Count)
  Next i
End Sub
[/color]

AFAIK there is nothing special about 59 sheets. Perhaps you are running out of memory?

 
Zathras and Skip,

I figured it out. For some strange reason, when the original sheet that's being copied has "Rows to repeat at the top" setup in Page Setup, the macro will only copy it a limited amount of times. When I clear the "Rows to repeat at the top" field, it makes as many copies as I want.

Thanks for your input and advice. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top