+ Reply to Thread
Results 1 to 6 of 6

Copy template sheet to a new sheet with the code in the teimplate sheet

  1. #1
    Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    MS-Off Ver
    2016
    Posts
    807

    Copy template sheet to a new sheet with the code in the teimplate sheet

    I have a template sheet in a workbook. Included in the template sheet is VBA which is visible in "Show Code" on right-clicking the tab. The code is designed to name the sheet after a cell reference the user selects in cell R1 and is trigged by a Change in the target cell (R1).

    I want the user to be able to copy the template sheet, paste it into a new sheet, make the selection in R1, and then have the VBA rename the tab to the choice in R1. It works fine in the template but the code is obviously not being included in the copy and paste to the new sheet.

    My lack of skills in VBA is obvious - is it possible to include the code when copying and pasting the template sheet?

    I have attached the workbook.

    Thanks for any help.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Copy template sheet to a new sheet with the code in the teimplate sheet

    From a coding standpoint, I don't think your approach is ideal. Copying the visible sheet and pasting it won't carry over any VBA. On top of that issue, I think the worksheet event you're using to apply the new name leaves a bit of room for improvement. Have you considered just using a command button, rather than trying to key everything off of changes in R1? In the attachment, I created a command button next to R1 on the Template sheet that creates a new sheet based on the template and using the name in R1. The code is as follows:


    NOTE: You should remove your existing code before clicking the button to run the code below, otherwise they'll get in each other's way

    Please Login or Register  to view this content.
    The current version will create a complete copy including the "Create New Sheet" button. This will enable you to create more new sheets based on whichever sheet you click the "Create new sheet" button from. If that is not an effect that you want, remove the apostrophe from the line noted in the code and the newly created sheets will not have the command button to create more new sheets, meaning the user can only create sheets from the template page. I hope that makes sense? I would advise removing the apostrophe and ensuring that all new sheets come from the template sheet only, but I couldn't tell from your post if you wanted the ability to duplicate sheets other than the template sheet. Take a look at the attachment to see if it's instructive:
    Attached Files Attached Files
    If your problem has been solved, please use "Thread Tools" to mark the thread as "Solved".

    If you're grateful for this site's existence and would like to contribute to the community, please consider posting something hilarious in our joke thread:
    https://www.excelforum.com/the-water...ke-thread.html

  3. #3
    Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    MS-Off Ver
    2016
    Posts
    807

    Re: Copy template sheet to a new sheet with the code in the teimplate sheet

    Hi

    Thank you for your help. I will run this through Monday and come back. Your assistance is much appreciated.

  4. #4
    Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    MS-Off Ver
    2016
    Posts
    807

    Re: Copy template sheet to a new sheet with the code in the teimplate sheet

    Hi CAntosh

    The code works beautifully - I can see your approach is far better than the one I was trying. Thanks for your help - I very much appreciate.

  5. #5
    Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    MS-Off Ver
    2016
    Posts
    807

    Re: Copy template sheet to a new sheet with the code in the teimplate sheet

    And you were 100% right that I didn't want the button copied so thanks for your forethought!

  6. #6
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Copy template sheet to a new sheet with the code in the teimplate sheet

    Glad to help, good luck!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. code to copy first rows and sheet titles from separate csv files into summary sheet
    By freshmint in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-08-2021, 06:10 AM
  2. [SOLVED] Copy Template Sheet and rename the new sheets based on a list in another sheet
    By SaadKiji in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-05-2016, 11:47 AM
  3. Move data to new sheet and create copy of template sheet
    By jdm13 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-25-2014, 01:53 PM
  4. Replies: 1
    Last Post: 01-04-2014, 06:37 PM
  5. [SOLVED] Copy a sheet (from template) to a new sheet, and rename (based on cell value)
    By Siglen in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-30-2012, 10:19 AM
  6. [SOLVED] Worksheet Activate when user selects sheet NOT when sheet is created from a template sheet
    By y0rk1e72 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-26-2012, 02:47 PM
  7. Replies: 11
    Last Post: 10-14-2012, 01:03 PM

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