Consulting

Results 1 to 6 of 6

Thread: Solved: Copying coded worksheets

  1. #1
    VBAX Regular
    Joined
    Jul 2006
    Posts
    26
    Location

    Solved: Copying coded worksheets

    I have a workbook with a sheet with VBA code attached. I now wish to create up to 25 additional sheets within the same workbook with the same code.

    If I do a paste special "all" the format is fine but it has no code attached

    What is the quickest way to make duplicate sheets within the same workbook?

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [vba]Sub copies()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    For i = 1 To InputBox("Enter number of copied")
    ws.Copy After:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Sheet" & Sheets.Count
    Next
    Set ws = Nothing
    End Sub
    [/vba]
    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'

  3. #3
    VBAX Regular
    Joined
    Jul 2006
    Posts
    26
    Location
    Thanks mdmackillop,

    I'm assuming I copy this code into the first worksheet and then when I create a new sheet all the code will be in place? or is this a bit too simplistic?

    Thanks, Peter.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Peter,
    Copy my code into a standard module. Run it from the worksheet you wish to duplicate. You don't need 25 copies of my code. If you need an example, let me know.
    Regards
    MD
    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'

  5. #5
    VBAX Regular
    Joined
    Jul 2006
    Posts
    26
    Location
    Thanks MD,

    That makes perfect sense. Appreciate the very quick reply.

    Regards, Peter.

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Peter

    What is this code?

    Is it worksheet event code?

    If so does it apply to every worksheet?

    If it does you might want to look at putting the code in the ThisWorkbook module.

Posting Permissions

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