+ Reply to Thread
Results 1 to 3 of 3

Copy Sheets from a template

Hybrid View

  1. #1
    Registered User
    Join Date
    01-15-2008
    Posts
    16

    Copy Sheets from a template

    Hello-

    I have a workbook in which I have a sheet labeled "Week 1" and a hidden sheet labeled "Template." The user enters a beginning date and an end date in a dialog box. I am using a date format, "ww," to determine the number of weeks between those dates. I then subtract the one week out to account for the "Week 1" sheet. I need to take that integer and create a loop to copy the hidden template sheet that number of times. The desired result is a workbook with tabs labeled "Week 1" through "Week x," determined by a beginning date and an end date. It looks something like this but I don't have it in front of me.
    Sub IDontKnowWhatImDoing
    
    Dim a as integer
    Dim b as integer
    Dim c as integer
    
    a = Format(txtStart.Text,ww)
    b = Format(txtEnd.Text,ww)
    
    c = (b-a)-1
    
    'It works up to this point.  c calculates.  Here's where I get confused.  I feel 'like it has do with not linking the counter up to the sheets
    
    For d = 2 to c
    Worksheets("Template").Visible = True
    Worksheets("Template").Copy
    Worksheets("Template").Visible = False 
    Next d
    
    End Sub

    Any help is appreciated. Thank you.
    Last edited by royUK; 07-12-2008 at 03:00 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I have added Code tags rhis time . Please read the Forum rules & use them in all future pots that require them.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Changing the Format will not return the week number. VBA does not support the WEEKNUM function so you need a UDF. place this in a Standard module

    'from Chip Pearson 
    'The  FW parameter serves the same purpose here as it does in the WEEKNUM  worksheet function.
    'Set it to 1 to indicate that weeks begin on Sunday, or to 2 to indicate that weeks begin on Monday.
    
    
    Function VBAWeekNum(D As Date, FW As Integer) As Integer
        VBAWeekNum = CInt(Format(D, "ww", FW))
    End Function
    For the userform

    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim w1     As Long
        Dim w2     As Long
        Dim w      As Long
        Dim X      As Long
    
        w1 = CLng(Me.txtStart.Value)
        w2 = CLng(Me.txtEnd.Value)
        X = w2 - w1
        Application.ScreenUpdating = True
        With Worksheets("Template")
            For w = 2 To X
                .Visible = xlSheetVisible
                .Copy after:=Worksheets(Worksheets.Count)
                ActiveSheet.Name = "Week " & w
                .Visible = xlSheetVeryHidden
            Next w
        End With
        Application.ScreenUpdating = False
    End Sub
    
    Private Sub txtEnd_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        With Me.txtEnd
            If Len(.Value) > 1 Then .Value = VBAWeekNum(CDate(.Value), 1)
        End With
    
    End Sub
    
    
    Private Sub txtStart_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        With Me.txtStart
            If Len(.Value) > 1 Then .Value = VBAWeekNum(CDate(.Value), 1)
        End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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