Results 1 to 5 of 5

Thread: "Copy method of Worksheet class Failed" - Excel VBA

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Location
    derby, uk
    Posts
    83

    "Copy method of Worksheet class Failed" - Excel VBA

    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 :-)

  2. #2

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Location
    derby, uk
    Posts
    83

    Re: "Copy method of Worksheet class Failed" - Excel VBA

    Further to the above.. I figured a dirty hack, simple have several reports, each with fewer than 26 result sets in it..

    went with ten, ran one ok. saved it..
    ran the second (another 10), gave it a new name, saved it
    the third bombed. as before, even though the origonal sheet is no longer open.

    it looks like worksheet(x).copy runs 'z' number of times and thats it without restarting excel... wierd.

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: "Copy method of Worksheet class Failed" - Excel VBA

    Dale
    I just ran your code 5 time on the same workbook with no errors. It produced 88 templates (44 * 2) each time without throwing any errors - so there nothing wrong with your code.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Re: "Copy method of Worksheet class Failed" - Excel VBA

    Quote Originally Posted by DKenny
    Dale
    I just ran your code 5 time on the same workbook with no errors. It produced 88 templates (44 * 2) each time without throwing any errors - so there nothing wrong with your code.
    I did the same. Code's working fine for me too.

    Think you can attach the workbook that's giving you the problem???




    If you find any of my posts of good help, please rate it

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Location
    derby, uk
    Posts
    83

    Re: "Copy method of Worksheet class Failed" - Excel VBA

    Ahem..

    Rename the attached to 'anything'.xls and it should be ok.

    the trick is to move to the 'control' sheet, select which set numbers you wish to generate a template for. then click the button, this will delete everything except the control sheet and the two templates, then build the templates as required.

    The set numbers relate to trains (yawn), for the purposes of testing the sheet the dates and period ranges are not relevent.

    This builds a report template which is then fed to a vb.net program to fill in.

    Its good to hear the code works, its probably something related to the machine here and something to do with a library being corrupt or some such rubbish.

    Typically it bombs around the 26th set on this machine, if it still all works fine its def this flipping machine.. Grrrr..

    Thanks for testing it though :-)
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width