+ Reply to Thread
Results 1 to 8 of 8

Trying to copy a set of sheets and rename them based on user input

  1. #1
    Registered User
    Join Date
    05-03-2019
    Location
    Chicago, IL
    MS-Off Ver
    365
    Posts
    10

    Trying to copy a set of sheets and rename them based on user input

    Hello,

    So I have a span of worksheets within a workbook, each name is in the format of "20 _______" except the first of the set, which is just "20"
    I want to copy this set of sheets, in order, and rename them based on the operation number the user puts in - so the next set should be 30.

    Currently, I get all the worksheets copied to the end of the sheets, which is correct, and the first sheet copied is correctly renamed, but the other sheets are not remained - they just get assigned "20 ______ (2)"

    I have also tried using
    Please Login or Register  to view this content.
    instead of
    Please Login or Register  to view this content.
    in the For Each/If loop, but that didn't work

    Here's the code:

    Please Login or Register  to view this content.

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Trying to copy a set of sheets and rename them based on user input

    try this

    Please Login or Register  to view this content.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Registered User
    Join Date
    05-03-2019
    Location
    Chicago, IL
    MS-Off Ver
    365
    Posts
    10

    Re: Trying to copy a set of sheets and rename them based on user input

    Quote Originally Posted by JLGWhiz View Post
    try this

    Please Login or Register  to view this content.
    So this works well for every copied sheet except for the one titled just "20".

    I tried writing an extra IF statement specifically to copy and rename the 20 page to opNum - this worked, but then I had two copies of 20, so I tried to add another condition to the original block that would check if ws.Name <> "20", but this failed to work.

    I'm curious why this fails to rename "20" to opNum. Any idea?

  4. #4
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,204

    Re: Trying to copy a set of sheets and rename them based on user input

    How about
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    05-03-2019
    Location
    Chicago, IL
    MS-Off Ver
    365
    Posts
    10

    Re: Trying to copy a set of sheets and rename them based on user input

    Yeah, this seems to work. My guess is you can't name a worksheet with an integer value, but concatenating the integer with a string coerces the type to string, which is why the one that was just =opNum didn't work. So the Variant type allows you to set just the opNum as the sheet name.

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,204

    Re: Trying to copy a set of sheets and rename them based on user input

    You can name a sheet using an integer, but the reason for using
    Please Login or Register  to view this content.
    is to remove the
    Please Login or Register  to view this content.
    that you had, as that line simply masks any errors that you were getting.

  7. #7
    Registered User
    Join Date
    05-03-2019
    Location
    Chicago, IL
    MS-Off Ver
    365
    Posts
    10

    Re: Trying to copy a set of sheets and rename them based on user input

    Cool, thank you. I know next to nothing about VBA so just trying to piece things together here.

  8. #8
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,204

    Re: Trying to copy a set of sheets and rename them based on user input

    Glad to help & thanks for the feedback

+ 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. Replies: 5
    Last Post: 04-08-2019, 11:33 AM
  2. Use VBA to create copies of hidden sheets based on user input with specific names
    By kcombs in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-06-2018, 01:09 PM
  3. Exporting multiple sheets to to Word based on User Input
    By rtcwlomax in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-01-2017, 06:20 AM
  4. Please Help with Copy/Paste and creating sheets depending on user input
    By rrstull in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-06-2013, 05:43 AM
  5. macro to copy a range of worksheets based on cell entry and rename specific sheets
    By Lbischoff in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-14-2012, 12:13 PM
  6. [SOLVED] Refreshing/Recalculating Sheets based on user input or switching between sheets
    By SonOfOdin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-24-2012, 09:48 PM
  7. Replies: 1
    Last Post: 08-27-2010, 02:59 AM

Tags for this Thread

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