Consulting

Results 1 to 4 of 4

Thread: Error copying multiple worksheets using VBA

  1. #1
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location

    Post Error copying multiple worksheets using VBA

    I'm trying to copy 2 sheets from the active workbook to a new workbook.

    This code gives an error, ' Method Copy of object 'Sheets' failed '

    but a new workbook "Book1" is created, but I can't see it or get to it.


           ChDir "c:\Temp1"
    
            b = MySht        ' b is the sheet name of  the  current month
        Sheets(Array("Sheet1", b)).Copy    [ This code gives an error here | ' Method Copy of object 'Sheets' failed ' 
             Set wb = ActiveWorkbook
             With wb
                .SaveAs fileName:="C:\Temp1\" & Sheets("Sheet1").Range("AX1").Text & " " & " The Closing Sheet" & ".xls"
                    
               .Close False
          End With

    I'm using office 2007. Is it true that you CANNOT copy a sheet with code inside of it ?

    Even when I delete the worksheet code, it doesn't work.
    The code previously worked when I only copied Sheet1

  2. #2
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    Think I might have figured it out.
    If I remove all of the Drawing Objects, the sheets copy as intended.

    I'll try it on some more sheets to see if that is precisely the problem.

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    b = MySht.Name
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    Apologies mdmackillop:

    Didn't see your post.

    When I removed the drawing objects, everything worked just fine.

    Does anyone have a method for copying multiple sheets to a new workbook if the sheets contain TextBoxes & other Drawing Objects.

    This process works if the sheets does not contain such objects;

        strSaveName = Worksheets("Sheet2").Range("a1").Value 
         
         ' copy sheets to new workbook
        Sheets(Array("Sheet1", "Sheet3")).Copy       ' [ This code gives an error here | ' Method Copy of object 'Sheets' failed '       
        ActiveWorkbook.SaveAs strSaveName
    However, if any of the sheets have objects in them, the copy process does not work

    Any ideas ?

Posting Permissions

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