+ Reply to Thread
Results 1 to 3 of 3

Copy worksheet template using command button

Hybrid View

  1. #1
    Registered User
    Join Date
    02-16-2008
    Posts
    9

    Question Copy worksheet template using command button

    Hello,
    I have created a worksheet which I am using as a template, "Main", and it is hidden in the workbook. I also created a starting page, "Info", to enter account information which contains a command button that when pushed copies "Main", and renames it appropriately, "Page 1", and sets the focus to "Page 1". The issue lies when I am on "Page 1", I am not sure how to make the command button on "Page 1" then copy "Main" and rename it to "Page 2". Since "Page 1" is copied from "Main", the command button matches that of "Main". I would like this to work on multiple pages. Thank you in advance for your help.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    What code are you using at the moment? I suspect you are referring to the activesheet when copying. Try this
    Option Explicit
    
    Private Sub CommandButton1_Click()
        '---------------------------------------------------------------------------------------
        ' Module    : Sheet3
        ' DateTime  : 08/02/2007 08:08
        ' Author    : Roy Cox (royUK)
        ' Website   :  more examples
        ' Purpose   : Add a sheet based on a hidden Main sheet into a specific position in the workbook
        ' Disclaimer; This code is offered as is with no guarantees. You may use it in your
        '             projects but please leave this header intact.
        '---------------------------------------------------------------------------------------
        Dim iWsCnt As Integer
        iWsCnt = ThisWorkbook.Worksheets.Count
        'hide operation from user
        Application.ScreenUpdating = False
    
        With ThisWorkbook.Worksheets("Main")
            .Visible = xlSheetVisible
            Select Case _
                   MsgBox("Click Yes to position the new sheet immediately before this one." _
                          & vbNewLine & vbNewLine & _
                          "Click No to place the new sheet at the end.", vbYesNo Or vbQuestion _
                                                                         Or vbDefaultButton1, "Add extra sheet")
    
                Case vbYes
                    ' add a sheet before the active sheet
                    .Copy before:=ActiveSheet
                Case vbNo
                    'add a sheet as last sheet
                    .Copy after:=Worksheets(iWsCnt)
            End Select
            'name the new sheet
            ActiveSheet.Name = "Page" & iWsCnt - 1    'you may need to change 1 according to the number of sheets not named page
            .Visible = xlSheetVeryHidden
        End With
    
        Application.ScreenUpdating = True
    
    End Sub
    Last edited by royUK; 03-02-2008 at 04:34 AM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    02-16-2008
    Posts
    9
    royUK,
    Thank you for the help. This works well but I did modify the code and removed the message box so the pages are automatically added to the end. Thank you agian for your help.
    Last edited by earl54dink; 03-04-2008 at 12:16 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1