+ Reply to Thread
Results 1 to 2 of 2

Problem with macro to copy then rename worksheets based on a list

Hybrid View

  1. #1
    Registered User
    Join Date
    10-08-2013
    Location
    Eugene, OR
    MS-Off Ver
    Excel 2010
    Posts
    43

    Problem with macro to copy then rename worksheets based on a list

    Hello - this is my first post, so I hope I explain the issue clearly so someone can help me. I have a spreadsheet with two sheets (Sheet1=BaseDate) and (Sheet2=BaseTemplate). Sheet1 contains a list of user defined dates. I want to copy Sheet2 (BaseTemplate) for each named date in Sheet1, then rename these newly copied sheets according to the list defined on Sheet1. I am using an InputBox method for the user to identify the range in Sheet1. My macro runs fine and it is creating a new sheet for each name contained on Sheet1, but it is renaming them as BaseTemplate incrementally numbered. I am attaching my original spreadsheet, my spreadsheet after my code runs, and my VBA code. I'm not that experienced in VBA code and actually copied different macros until I got it do to what I wanted.

    Original.xlsxAfter Macro.xlsx

    Sub CreateDates()
    
    Dim MyCell As Range
    Dim MyRange As Range
    Dim wsNew As Worksheet
    Dim wsBaseTemplate As Worksheet
    
    On Error Resume Next
            Application.DisplayAlerts = False
    
                Set MyRange = Application.InputBox(Prompt:="Please select the dates with your mouse.", Type:=8)
    
        On Error GoTo 0
    
            Application.DisplayAlerts = True
    
    Set wsBaseTemplate = Sheets("BaseTemplate")
    Application.ScreenUpdating = False
    
    MyRange.Select
    For Each MyCell In MyRange
    wsBaseTemplate.Copy After:=Sheets(Sheets.Count)
    Set wsNew = ActiveSheet
    On Error Resume Next
    wsNew.Name = MyCell.Value
    On Error GoTo 0
    Next MyCell
    
    Application.ScreenUpdating = True
    
    End Sub

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Problem with macro to copy then rename worksheets based on a list

    Try it this way:

    Option Explicit
    
    Sub CreateSheetsFromDates()
    Dim MyDates As Range, MyDate As Range
    
    'make a range of all cells in column A with numbers in them, dates are numeric
    Set MyDates = Sheets("BaseDates").Range("A:A").SpecialCells(xlConstants, xlNumbers)
    
    For Each MyDate In MyDates
        If Not Evaluate("ISREF('" & CStr(MyDate.Text) & "'!A1)") Then   'make sure sheet doesn't exist already
            Sheets("BaseTemplate").Copy After:=Sheets(Sheets.Count)     'copy template
            ActiveSheet.Name = CStr(MyDate.Text)                        'name the new sheet
        End If
    Next MyDate
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ 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. Macro - Copy sheet and rename based on cell value
    By Nick.123 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-15-2013, 04:23 AM
  2. 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
  3. Rename all worksheets created from macro according to list on Column A
    By ron2k_1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-25-2011, 06:29 PM
  4. Copy and rename based from list
    By realniceguy5000 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-31-2008, 01:23 PM
  5. [SOLVED] rename worksheets after copy
    By Qaspec in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-02-2005, 11:06 AM

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