+ Reply to Thread
Results 1 to 2 of 2

Duplicte and rename multiple sheets using VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    05-26-2022
    Location
    USA
    MS-Off Ver
    365
    Posts
    29

    Duplicte and rename multiple sheets using VBA

    I have a code below that duplicates and then renames the active sheet in excel using VBA. This works great thanks to MikeVol Now, I need to do several more things at the same time.

    I have a main schedule sheet "M-D-YY" and then two supporting sheets. "M-D-YY Block" and "M-D-YY Export" both of these sheets contain info the main sheet pulls from. Could the code below be used to duplicate and rename these as well?

    So this is the order in which things need to be done: (Steps one and two are solved I need help with steps 3-5)

    1. Duplicate the active sheet. ("M-D-YY")
    2. Rename it utilizing the actives sheets name. ("M-D-YY" plus 42 days)
    3. Duplicate the Block and Export sheets that support the active sheet. ("M-D-YY Block" and "M-D-YY Export")
    These sheets are hidden so the code would need to look at the active sheet name then duplicate the corresponding block and export sheets.
    4. Rename both the block and export sheets using the new date "M-D-YY plus 42 days"
    5. Find and replace references in the newly duplicated "M-D-YY" sheet to the newly created block sheet.

    Example:
    Active sheet is 5-6-24
    Steps 1-2 - duplicates the active sheet "5-6-24" and then renames it "6-17-24"
    Step 3-4 - Duplicates "5-6-24 Block" and 5-6-24 Export" and renames them to "6-17-24 Block" and 6-17-24 Export"
    Step 5 - Find and replace any references to 5-6-24 Block and replaces with 6-17-Block in the newly created sheet from steps 1-2

    Ideally this would all be done with one button click.

    Below is the working code for steps 1-2. BTW this code also unprotect and deletes some cells in the active sheet. That part of the code works great.




    Sub copy_and_delete_schedule()

    Application.ScreenUpdating = False

    Dim ws As Worksheet
    Set ws = ActiveSheet

    Dim currentDateString As String
    currentDateString = ws.Name
    currentDateString = Replace(currentDateString, "-", "/")
    currentDateString = Replace(currentDateString, " ", "")

    Dim currentDate As Date
    currentDate = DateValue(currentDateString)

    Dim newDate As Date
    newDate = DateValue(currentDate) + 42

    ' ' Or
    ' newDate = DateAdd("d", 42, currentDate)

    ws.Copy After:=ActiveSheet
    ActiveSheet.Name = Format(newDate, "m-d-yy")

    With ActiveSheet
    .Unprotect "unicorn"
    .Range("BP2,O25:BI37,O44:BI56,O63:BI71,O82:BI91,O102:BI108,O123:BI132,O139:BI144,O149:BI159,O164:BI172,O174:BI180,O182:BI184,O189:BI195,O197:BI200,O206:BI211,O213:BI215,O217:BI221,O228:BI233").ClearContents

    With .Range("M25:M37,O25:BI37,M44:M56,O44:BI56,M63:M171,O63:BI71,M82:M191,O82:BI91,M102:M108,O102:BI108,M123:M132,O123:BI132,M139:M144,O139:BI144,M149:M159,O149:BI159,M164:M172,O164:BI172,M174:M180,O174:BI180")
    .Locked = False
    .FormulaHidden = False
    End With

    With .Range("M182:M184,O182:BI184,M189:M195,O189:BI195,M197:M200,O197:BI200,M206:M211,O206:BI211,M213:M215,O213:BI215,M217:M221,O217:BI221,M228:M233,O228:BI233")
    .Locked = False
    .FormulaHidden = False
    End With

    .Protect "unicorn"
    Application.Goto .Cells(1, 1), True

    Application.ScreenUpdating = False

    End With

    End Sub

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,008

    Re: Duplicte and rename multiple sheets using VBA

    Sub TestMacro()
        'Active Sheet Is 5 - 6 - 24
        'Steps 1-2 - duplicates the active sheet "5-6-24" and then renames it "6-17-24"
        'Step 3-4 - Duplicates "5-6-24 Block" and 5-6-24 Export" and renames them to "6-17-24 Block" and 6-17-24 Export"
        'Step 5 - Find and replace any references to 5-6-24 Block and replaces with 6-17-Block in the newly created sheet from steps 1-2
        
        Dim shA As Worksheet
        Dim strOld As String
        Dim strNew As String
        Dim dteA As Date
        
        Set shA = ActiveSheet
        strOld = ActiveSheet.Name
        On Error GoTo ErrHandler
        dteA = DateValue(strOld)
        strNew = Format(dteA + 42, "m-d-yy")
        
        shA.Copy After:=Worksheets(strOld & " Export")
        Worksheets(Worksheets(strOld & " Export").Index + 1).Name = strNew
        Worksheets(strOld & " Block").Copy After:=Worksheets(strNew)
        Worksheets(Worksheets(strNew).Index + 1).Name = strNew & " Block"
        Worksheets(strOld & " Export").Copy After:=Worksheets(strNew & " Block")
        Worksheets(Worksheets(strNew & " Block").Index + 1).Name = strNew & " Export"
        
        With Worksheets(strNew).Cells
            .Replace strOld & " Block", strNew & " Block", LookAt:=xlPart
        End With
        
        Exit Sub
    ErrHandler:
        MsgBox "The active sheet must have a date-based name for this macro to work."
        
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

+ 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. rename multiple sheets at once
    By stephme55 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-31-2016, 12:51 PM
  2. rename multiple sheets with vba
    By stephme55 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-26-2016, 01:56 PM
  3. How to Rename Multiple Sheets using VBA
    By bgetz12 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-19-2014, 11:32 AM
  4. Excel 2007 : Required Macro for rename multiple sheets
    By yogeshkaushik in forum Excel General
    Replies: 1
    Last Post: 07-29-2011, 02:23 AM
  5. [SOLVED] How do I rename multiple sheets at one time in Excel?
    By Navigator in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-25-2006, 03:00 PM
  6. [SOLVED] Rename multiple sheets
    By al in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-30-2005, 09:05 AM
  7. Rename Multiple Sheets from a List of Available Names
    By prkhan56 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-04-2005, 02:06 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