Consulting

Results 1 to 6 of 6

Thread: Copy to new WorkSheet and name as specific dates

  1. #1
    VBAX Regular
    Joined
    Jun 2007
    Location
    Texas
    Posts
    62
    Location

    Copy to new WorkSheet and name as specific dates

    Am trying to do the following based on the answers to the beginning and ending date questions
    • Create copies of the original sheet named "Master"
    • Rename the tabs the correct dates and eliminate the weekends
    • And if possible, put the date, ie name of tab, in cell K2


    I can create the tabs and name the dates, but didn't copy the information from the original tab. Here is what I have so far. I appreciate your assistance.


    Sub AddDatedWS()
    Dim strStartDt As String
    Dim strEndDt As String
    Dim dtStart As Date
    Dim dtEnd As Date
    Dim wsNew As Worksheet
    Dim n As Double
    ' Turns the screen off while the Macro is running.
    Application.ScreenUpdating = False
    'get start date
    strStartDt = InputBox("Enter start date", "Create dated worksheets")
    If Not IsDate(strStartDt) Then Exit Sub
    'get end date
    strEndDt = InputBox("Enter end date", "Create dated worksheets")
    If Not IsDate(strStartDt) Then Exit Sub
    
    
    'convert text to Excel's date format
    dtStart = CDate(strStartDt)
    dtEnd = CDate(strEndDt)
    'test if start date equal to or later than end date
    If dtStart >= dtEnd Then Exit Sub
    
    
    'confirm number of sheets
    If MsgBox("Create " & dtEnd - dtStart + 1 & " worksheets", vbOKCancel) = _
    vbCancel Then Exit Sub
    
    
    For n = dtStart To dtEnd
    'create a new worksheet
    Set wsNew = ActiveWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count))
    'name it with a date (date text can't contain : \ / ? * [ or ])
    wsNew.Name = Format(n, "mm.dd.yy")
    Next n
    'Turns the screen back on when the Macro is finished.
    Application.ScreenUpdating = True
    End Sub

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub AddDatedWS()
        Dim strStartDt As String
        Dim strEndDt As String
        Dim dtStart As Date
        Dim dtEnd As Date
        Dim wsNew As Worksheet
        Dim n As Double
         ' Turns the screen off while the Macro is running.
        Application.ScreenUpdating = False
         'get start date
        strStartDt = InputBox("Enter start date", "Create dated worksheets")
        If Not IsDate(strStartDt) Then Exit Sub
         'get end date
        strEndDt = InputBox("Enter end date", "Create dated worksheets")
        If Not IsDate(strStartDt) Then Exit Sub
         
         
         'convert text to Excel's date format
        dtStart = CDate(strStartDt)
        dtEnd = CDate(strEndDt)
         'test if start date equal to or later than end date
        If dtStart >= dtEnd Then Exit Sub
         
         
         'confirm number of sheets
        If MsgBox("Create " & dtEnd - dtStart + 1 & " worksheets", vbOKCancel) = _
        vbCancel Then Exit Sub
         
         'Amended code @@@@@@@@@@@@@@@@@@@@@@ 
        For n = dtStart To dtEnd
            If Weekday(n) <> 1 And Weekday(n) <> 7 Then
             'create a new worksheet
            Set wsNew = ActiveWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count))
             'name it with a date (date text can't contain : \ / ? * [ or ])
            wsNew.Name = Format(n, "mm.dd.yy")
            Sheets("Master").UsedRange.Copy wsNew.Range("A1")
            wsNew.Range("K2") = Format(n, "mm.dd.yy")
            End If
        Next n
         'Turns the screen back on when the Macro is finished.
        Application.ScreenUpdating = True
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Jun 2007
    Location
    Texas
    Posts
    62
    Location
    This works great with a small complication. The new sheets that are created are not in the same format as the original. For instance, the columns are different widths than the original sheet so they have to be widened.

    I have added a couple of lines to the macro, but am still not there yet.

    Sub Add_Dates_of_Attendance()
    Dim strStartDt As String
    Dim strEndDt As String
    Dim dtStart As Date
    Dim dtEnd As Date
    Dim wsNew As Worksheet
    Dim n As Double
    ' Turns the screen off while the Macro is running.
    Application.ScreenUpdating = False
    'get start date
    strStartDt = InputBox("Enter start date", "Create dated worksheets")
    If Not IsDate(strStartDt) Then Exit Sub
    'get end date
    strEndDt = InputBox("Enter end date", "Create dated worksheets")
    If Not IsDate(strStartDt) Then Exit Sub


    'convert text to Excel's date format
    dtStart = CDate(strStartDt)
    dtEnd = CDate(strEndDt)
    'test if start date equal to or later than end date
    If dtStart >= dtEnd Then Exit Sub


    'confirm number of sheets
    If MsgBox("Create " & dtEnd - dtStart + 1 & " worksheets", vbOKCancel) = _
    vbCancel Then Exit Sub

    'Amended code @@@@@@@@@@@@@@@@@@@@@@
    For n = dtStart To dtEnd
    If Weekday(n) <> 1 And Weekday(n) <> 7 Then
    'create a new worksheet
    Set wsNew = ActiveWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count))
    'name it with a date (date text can't contain : \ / ? * [ or ])
    wsNew.Name = Format(n, "mm.dd.yy")
    Sheets("Master").UsedRange.Copy wsNew.Range("A1")
    wsNew.Range("K2") = Format(n, "mm.dd.yy")

    ActiveWindow.View = xlPageBreakPreview
    ActiveWindow.Zoom = 100
    ActiveSheet.PageSetup.Orientation = xlLandscape

    End If
    Next n

    'Turns the screen back on when the Macro is finished.
    Application.ScreenUpdating = True
    End Sub

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Save a blank hidden template sheet and make a copy of it to create your new sheets.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Jun 2007
    Location
    Texas
    Posts
    62
    Location
    Quote Originally Posted by mdmackillop View Post
    Save a blank hidden template sheet and make a copy of it to create your new sheets.
    Not sure how that helps....

    PAM

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    The blank sheet can be set up with your columns widths, headers, layout and any formatting you require.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •