Consulting

Results 1 to 14 of 14

Thread: Solved: Copy and rename sheet in different name

  1. #1

    Solved: Copy and rename sheet in different name

    Hi there,

    I want to copy my "Master" sheet to new wb and rename it as "Simulation1", "Simulation2" and so on. But here I only need to copy table format, value format and without formula.

    Below is the code from Simon referring to thread http://www.vbaexpress.com/forum/show...t+to+new+sheet

    But this code give me error '1004'. Hope some one could help me on this.

    PHP Code:
    Sub CopySheetAll()
        
    Dim Master As String
        MyBook 
    ActiveWorkbook.Name
        ActiveSheet
    .Copy
        ActiveSheet
    .PasteSpecial Paste:=xlPasteAllOperation:=xlNoneSkipBlanks:= _
            False
    Transpose:=False
        Application
    .CutCopyMode False
        ActiveWorkbook
    .SaveAs ("Simulation" ".xls")
        
    Workbooks(Master).Activate
    End Sub 
    Last edited by halimi1306; 04-20-2011 at 03:41 AM.

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    From your present code, you should error at:
        ActiveSheet.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, 
    SkipBlanks:= _
            False, Transpose:=False
    ...as there's nothing on the clipboard to paste.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try this instead

    [vba]

    Sub CopySheetAll()
    Dim Master As String
    Dim MyBook As String
    MyBook = ActiveWorkbook.Name
    ActiveSheet.UsedRange.Copy
    ActiveSheet.UsedRange.Cells(1, 1).PasteSpecial _
    Paste:=xlPasteValuesAndNumberFormats, _
    Operation:=xlNone, _
    SkipBlanks:=False, _
    Transpose:=False
    Application.CutCopyMode = False
    ActiveWorkbook.SaveAs ("Simulation" & ".xls")
    Workbooks(Master).Activate
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    hi,
    Attached herewith is the sample file. I want the result from sheet "Model" to be copied in new workbook and need to be rename as "Simulation". I need to run this for few time and rename it i.e. "Simulation1" , "Simulation2" and so on. Pls have a look at my file for better understanding.
    CPI Model v.6.0.xls

  5. #5
    Quote Originally Posted by xld
    Try this instead
    I got error 1004, and how to copy table format? Pls have a look at my attachment.

    Thank xld

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    I am reading your request a bit differently than Bob is. I took it that you want to copy the sheet to a new wb, and just retain the values therein (the new wb). I got this far, but your sheet is protected and w/a password...

    Could you reload the attachement or tell us the sheet's password?
    Sub exa()
    Dim wks As Worksheet
    Dim wb As Workbook
    Dim Path As String
        
        Path = ThisWorkbook.Path & "\"
        
        Set wks = ThisWorkbook.Worksheets("Model")
        Set wb = Workbooks.Add(xlWorksheet)
        wks.Copy After:=wb.Worksheets(1)
        Application.DisplayAlerts = False
        wb.Worksheets(1).Delete
        Application.DisplayAlerts = True
        
        Set wks = wb.Worksheets(1)
        
        'unprotect or protect w/UserInterfaceOnly here
        
        wks.UsedRange.Value = wks.UsedRange.Value
        
        wb.SaveAs Path & "Simulation.xls"
    End Sub

  7. #7
    Quote Originally Posted by GTO
    I am reading your request a bit differently than Bob is. I took it that you want to copy the sheet to a new wb, and just retain the values therein (the new wb). I got this far, but your sheet is protected and w/a password...

    Could you reload the attachement or tell us the sheet's password?
    [code]Sub exa()
    Ops! Sorry.

    pasword: asd

    I try to run your code but it return error.
    one more, if you see my sheet, there's a tab name Dashboard. Is it possible to copy model tab and dashboard in same new wb because those two is the result from each simulation. THe Dashboard tab is link to model tab.
    Last edited by halimi1306; 04-20-2011 at 04:32 AM.

  8. #8
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    In a junk copy of your wb, try the code with wks.Protect "asd", , , , True added below the comment. I cannot really test, as IFERROR is not available in 2003. I can open your attached wb and the values show fine, but if I copy the sheet, everything goes #N/A

  9. #9
    Quote Originally Posted by GTO
    In a junk copy of your wb, try the code with wks.Protect "asd", , , , True added below the comment. I cannot really test, as IFERROR is not available in 2003. I can open your attached wb and the values show fine, but if I copy the sheet, everything goes #N/A
    It works great! But if I run for second round, how to rename the sheet as "Simulation 2" and so on for 3rd run...

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It worked fine for me using Excel 20120.

    Checkout SaveCopyAs in VBA Help, this will copy the workbook, then delete all sheets except your nominated sheets.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    I could't find VBA Help, would you mind to give a link

  12. #12
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by xld
    It worked fine for me using Excel 20120.
    Wow! How many rows in that version?

  13. #13
    hehehe... I think typo error. Maybe 2010

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by GTO
    Wow! How many rows in that version?
    Its a dogfood release!
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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