problem:
I'm trying to copy a wide range of sheets under macro control, well actually two sheets being copied & configured to make a report template.

The code works fine, upto a point.
VB Code:
  1. Sub BuildTemplate()
  2.     ' scan set list looking for sets to include
  3.     Dim Row As Integer
  4.     Dim MySheet As Worksheet
  5.    
  6.     Dim junk As String
  7.    
  8.     ' remove all sheets *except* the control sheet and the templates
  9.     Application.DisplayAlerts = False
  10.     For Each MySheet In Application.Worksheets
  11.         If MySheet.Name <> "Controls" And MySheet.Name <> "Template-3" And MySheet.Name <> "Template-4" Then
  12.             MySheet.Delete
  13.         End If
  14.     Next
  15.     Application.DisplayAlerts = True
  16.    
  17.    
  18.         For Row = 4 To 47
  19.             If Worksheets("Controls").Cells(Row, 1) = "Yes" Then
  20.                 ' set is marked to copy
  21.                
  22.                 Worksheets("Template-3").Copy before:=Worksheets("Template-3")
  23.                 Set MySheet = Worksheets("Template-3 (2)")
  24.                 MySheet.Range("d6") = Worksheets("Controls").Cells(Row, 2)
  25.                 MySheet.Range("h6") = Worksheets("Controls").Range("d4")
  26.                 MySheet.Range("d8") = Worksheets("Controls").Range("d7")
  27.                 MySheet.Name = Worksheets("Controls").Cells(Row, 2) & "-Data"
  28.                
  29.                 Worksheets("Template-4").Copy before:=Worksheets("Template-3")
  30.                 Set MySheet = Worksheets("Template-4 (2)")
  31.                 MySheet.Range("C2") = Worksheets("Controls").Cells(Row, 2)
  32.                 MySheet.Range("E2") = Worksheets("Controls").Range("d4")
  33.                 MySheet.Range("C3") = Worksheets("Controls").Range("d7")
  34.                 MySheet.Name = Worksheets("Controls").Cells(Row, 2) & "-Summary"
  35.             End If
  36.  
  37.         Next Row
  38.    
  39.     MsgBox ("Templates Created for Selected Sets")
  40.    
  41. End Sub

It runs, and runs well for small numbers of sheet to copy,

the 'controls' page holds a two column list, where by a 'yes' or 'no' is specified next to the target sheets name, it copies the two template pages, configures them and renames them, then moves on to the next one.

so far its producing around 26 copies ok, then throwing an error.

Runtime Error 1004
"Copy method of Worksheet class Failed"

the help system being somewhat useless with messages like this, the debug window points to one of the two 'worksheet(x).copy' lines, not always the same one. Its not the sheet names either since it doesn't matter which of the records I select it gets so far then dies.

Given this function has worked fine upto this point, building the first 26 copies fine this is somewhat confusing.

this alos nukes the copy function totally for coping worksheets manually, it just doesn't do anything until excel is closed & restarted. Deleting sheets manually doesn't help so I doubt its an internal sheet limit (is there a limit on how many sheets you can have?)

any ideas on what could be causing this?

naturally an ideas on how to resolve it would also be welcome :-)