+ Reply to Thread
Results 1 to 10 of 10

Button to copy sheet, rename sheet sequencially.

  1. #1
    Registered User
    Join Date
    05-23-2005
    Posts
    25

    Button to copy sheet, rename sheet sequencially.

    I have a Setup sheet, which has a button to creat a new sheet, which actually copies a tamplet. When i click this button it calls the new tab "Tamplet (2)"
    Is there a sub i can put in the copy routine to auto-rename the tabs sequencially?
    Note number of tabs is based upon how many times the user clicks the new page button.

    Thanks!
    ~Josh

  2. #2
    Patrick Molloy
    Guest

    RE: Button to copy sheet, rename sheet sequencially.

    Option Explicit

    Public Sub AddSheets()
    Dim ws As Worksheet
    Set ws = Worksheets("template")
    ws.Copy Worksheets(1)
    SetSheetName Worksheets(1)
    End Sub
    Private Sub SetSheetName(ws As Worksheet)
    On Error Resume Next
    Dim sname As String
    Dim index As Long
    index = 0
    Do
    Err.Clear
    index = index + 1
    sname = "template" & Format$(index, "000")
    ws.Name = sname
    Loop While Err.Number <> 0
    End Sub

    "foxgguy2005" wrote:

    >
    > I have a Setup sheet, which has a button to creat a new sheet, which
    > actually copies a tamplet. When i click this button it calls the new
    > tab "Tamplet (2)"
    > Is there a sub i can put in the copy routine to auto-rename the tabs
    > sequencially?
    > Note number of tabs is based upon how many times the user clicks the
    > new page button.
    >
    > Thanks!
    > ~Josh
    >
    >
    > --
    > foxgguy2005
    > ------------------------------------------------------------------------
    > foxgguy2005's Profile: http://www.excelforum.com/member.php...o&userid=23663
    > View this thread: http://www.excelforum.com/showthread...hreadid=378885
    >
    >


  3. #3
    Registered User
    Join Date
    05-23-2005
    Posts
    25

    Exclamation

    I tried putting this into my command button as follows:
    but it does not work, i'm not quite sure what i'm doing wrong here?
    Thanks!
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    05-23-2005
    Posts
    25

    Exclamation

    anyone help me out on this one, much appreaciate it!

  5. #5
    Dave Peterson
    Guest

    Re: Button to copy sheet, rename sheet sequencially.

    I put a commmandbutton on a worksheet, double clicked on that commandbutton and
    pasted your code.

    It worked right out of the box.

    If you're using xl97, try changing the commandbutton's .takefocusonclick
    property to false.

    If that's not it, what happened when you tried it?

    foxgguy2005 wrote:
    >
    > I tried putting this into my command button as follows:
    > but it does not work, i'm not quite sure what i'm doing wrong here?
    > Thanks!
    >
    > Code:
    > --------------------
    > Private Sub CommandButton1_Click()
    > Dim ws As Worksheet
    > Set ws = Worksheets("Tamplet")
    > ws.Copy Worksheets(1)
    > SetSheetName Worksheets(1)
    > End Sub
    > Private Sub SetSheetName(ws As Worksheet)
    > On Error Resume Next
    > Dim sname As String
    > Dim index As Long
    > index = 0
    > Do
    > Err.Clear
    > index = index + 1
    > sname = "Tamplet" & Format$(index, "000")
    > ws.Name = sname
    > Loop While Err.Number <> 0
    >
    > End Sub
    > --------------------
    >
    > --
    > foxgguy2005
    > ------------------------------------------------------------------------
    > foxgguy2005's Profile: http://www.excelforum.com/member.php...o&userid=23663
    > View this thread: http://www.excelforum.com/showthread...hreadid=378885


    --

    Dave Peterson

  6. #6
    Registered User
    Join Date
    05-23-2005
    Posts
    25

    Talking

    yeah you know it works for mee to actually, i'm retarded, lol.

    Thanks man, tweaked the code for only 2 index numbers and to not include the tamplet... works like a charm, you are the MAN!

  7. #7
    Dave Peterson
    Guest

    Re: Button to copy sheet, rename sheet sequencially.

    I'd say Patrick was the man!

    foxgguy2005 wrote:
    >
    > yeah you know it works for mee to actually, i'm retarded, lol.
    >
    > Thanks man, tweaked the code for only 2 index numbers and to not
    > include the tamplet... works like a charm, you are the MAN!
    >
    > --
    > foxgguy2005
    > ------------------------------------------------------------------------
    > foxgguy2005's Profile: http://www.excelforum.com/member.php...o&userid=23663
    > View this thread: http://www.excelforum.com/showthread...hreadid=378885


    --

    Dave Peterson

  8. #8
    Registered User
    Join Date
    05-23-2005
    Posts
    25
    heh yeah, actually thats what i meant! :-P
    Thanks big pat!
    you too dave, for making me check myself.

  9. #9
    Registered User
    Join Date
    05-23-2005
    Posts
    25
    an another quick note is there a way to copy them before a certain sheet
    IE: Before:=Sheets("Setup")

  10. #10
    Dave Peterson
    Guest

    Re: Button to copy sheet, rename sheet sequencially.

    Yep.

    Option Explicit
    Public Sub AddSheets()
    Dim ws As Worksheet
    Set ws = Worksheets("template")
    ws.Copy _
    before:=Worksheets("setup")
    SetSheetName ActiveSheet
    End Sub
    Private Sub SetSheetName(ws As Worksheet)
    On Error Resume Next
    Dim sname As String
    Dim index As Long
    index = 0
    Do
    Err.Clear
    index = index + 1
    sname = "template" & Format$(index, "000")
    ws.Name = sname
    Loop While Err.Number <> 0
    End Sub


    When you copy a sheet, it becomes the activesheet. So we just pass the
    activesheet to the "setSheetName" subroutine.



    foxgguy2005 wrote:
    >
    > an another quick note is there a way to copy them before a certain
    > sheet
    > IE: Before:=Sheets("Setup")
    >
    > --
    > foxgguy2005
    > ------------------------------------------------------------------------
    > foxgguy2005's Profile: http://www.excelforum.com/member.php...o&userid=23663
    > View this thread: http://www.excelforum.com/showthread...hreadid=378885


    --

    Dave Peterson

+ 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