+ Reply to Thread
Results 1 to 7 of 7

Thread: Auto-generate new weekly workbook based on a saved template

  1. #1
    Registered User
    Join Date
    05-30-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    2

    Smile Auto-generate new weekly workbook based on a saved template

    Hi,

    I would really grateful if someone could help with the issue I am having with Excel 07.

    I have created and saved a template in excel 07 that contains macros for sorting data, and vlookups for analysing the data. This data is added daily.

    The template has 5 tabs for data for each day of the week, and a sixth tab for analysing that weekly data.

    Is there a bit of code that could be added to the template that auto-generates a new workbook based on the above each week, with all the macros etc in template, minus the data.

    If someone could advise how that is possible it would be much appreciated.

    thanks,

    James

  2. #2
    Valued Forum Contributor tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    USA
    MS-Off Ver
    Excel 2003 - 2007
    Posts
    2,352

    Re: Auto-generate new weekly workbook based on a saved template

    James,

    Welcome to the forum.

    What you're requesting can be accomplished, the code would just need to know what fields need clearing, where to save the new workbook (if different from the current workbook's folder) and what to name the new workbook. The code would look something like this:
    Sub CreateNew()
        
        'Change these ranges as necessary
        Dim WeekdayFieldsToClear As String:     WeekdayFieldsToClear = "A2:D10, F12:G15, J5, J8, I2:K5"
        Dim AnalysisFieldsToClear As String:    AnalysisFieldsToClear = "J10, J12, I2:I12"
        
        Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
            'Change the workday sheet names as necessary
            If ws.Name = "Monday" _
            Or ws.Name = "Tuesday" _
            Or ws.Name = "Wednesday" _
            Or ws.Name = "Thursday" _
            Or ws.Name = "Friday" Then
            
                ws.Range(WeekdayFieldsToClear).ClearContents
                
            'Change the analysis sheet name as necessary
            ElseIf ws.Name = "Analysis" Then
            
                ws.Range(AnalysisFieldsToClear).ClearContents
                
            End If
        Next ws
        
        'Currently the file will be saved in the same folder as the active workbook
        'Change the FilePath if necessary. Make sure to include the ending \
        Dim FilePath As String: FilePath = ActiveWorkbook.Path & "\"
        
        'This prompts the user to input the name of the new workbook
        Dim NewName As String:  NewName = vbNullString
        While NewName = vbNullString
            NewName = InputBox(Title:="New Filename", _
                               Prompt:="Type a name for the new workbook to be saved as.")
            
        Wend
        
        'Saves the now blank template as the new filename in the specified path, as a .xls file
        ActiveWorkbook.SaveAs Filename:=FilePath & NewName & ".xls", _
                              FileFormat:=xlExcel8
        
    End Sub


    Hope that helps,
    ~tigeravatar

  3. #3
    Valued Forum Contributor p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2003-2010
    Posts
    377

    Re: Auto-generate new weekly workbook based on a saved template

    an easier way to erase you old data would be to name your data range for each sheets and perform a ClearContents for each of those ranges.
    You can manage to create a dynamic range name that expand with the data you input.

    that would replace the first part of the macro suggested by Tiger.

    The rest of the macro Tiger wrote is still valid.
    Pierre

    Range(Monday_data).ClearContents
    Range(Tuesday_data).ClearContents
    Range(Wednesday_data).ClearContents
    Range(Thursday_data).ClearContents
    Range(Friday_data).ClearContents
    Range(Analysis_data).ClearContents

  4. #4
    Valued Forum Contributor p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2003-2010
    Posts
    377

    Re: Auto-generate new weekly workbook based on a saved template

    an even simpler solution is to keep a BLANK workbook in a directory and open it each time you need to start a new week. Don't forget to change the name of the file the first time you save it. You could protect the BLANK workbook so you wont mess with it or make it read only. This will force you to save it with a new name.
    Pierre

  5. #5
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Auto-generate new weekly workbook based on a saved template

    The easiest way would be to create a proper Excel Template(*.xltm). See

    Excel templates
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  6. #6
    Registered User
    Join Date
    05-30-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Auto-generate new weekly workbook based on a saved template

    Guys,

    thanks so much for your help, I wasnt expecting such a speedy response, much appreciated.

    James

  7. #7
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Auto-generate new weekly workbook based on a saved template

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0