+ Reply to Thread
Results 1 to 5 of 5

Creating new sheets based off template

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel for Office 365
    Posts
    1,702

    Creating new sheets based off template

    Hi,

    Can anyone help me out with some code which will really save me a lot of time.

    I have a summary sheet which lists unique codes in column D. For each code listed in the list I will need a new worksheet created based off the template.
    e.g.on the attached the list on the summary sheet is:
    • 213000
    • 500000
    • 662200


    So I would need it to create 3 sheets all based off the “template” sheet. The template sheet has the formulas which I need replicating on all of the new sheets created.

    On each new sheet if I could have it so the sheet name is named after the account e.g. “213000” and also in B2 of each sheet it also has the account name e.g on sheet 213000 in B2 it has “213000” which means its picked up on the summary sheet.

    In my actual data I have lots of account so if there is any way it could look to the list of accounts on the summary sheets and start at cell D5 until the last row rather than fixing the range that will help.

    Can anyone help please?
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Creating new sheets based off template

    Hello,

    Try this code and see if it works. I hope it helps

    Sub CreateSheets()
        
        Const lFIRST_ROW_WITH_DATA As Long = 5
        
        Dim shTemplate As Worksheet
        Dim shSummary As Worksheet
        Dim lLastRow As Long
        Dim i As Long
            
        ' Turn extras off to make the code run faster.
        ' You need this there is quite a few array formulas
        Call TurnExtasOff
        
        ' Asign the variables.
        Set shTemplate = ActiveWorkbook.Sheets("Template")
        Set shSummary = ActiveWorkbook.Sheets("Summary")
        lLastRow = shSummary.Cells(shSummary.Rows.Count, "C").End(xlUp).Row
        
        ' Loop through all the rows and grab the data.
        For i = lFIRST_ROW_WITH_DATA To lLastRow
            
            ' Make sure the cell is not empty
            If Not shSummary.Cells(i, 3).Value = vbNullString Then
                ' Copy the template
                shTemplate.Copy After:=Sheets(Sheets.Count)
                            
                ' Change the name and add the value to the cell.
                ActiveSheet.Name = shSummary.Cells(i, 3).Value
                ActiveSheet.Range("B2").Value = shSummary.Cells(i, 3).Value
            End If
        Next i
        
        ' Turn extras back on
        Call TurnExtasOn
        
        ' Clean up
        Set shTemplate = Nothing
        Set shSummary = Nothing
       
        
    End Sub
    
    Sub TurnExtasOff()
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
            .Calculation = xlCalculationManual
        End With
    End Sub
    
    Sub TurnExtasOn()
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = xlCalculationAutomatic
        End With
    End Sub
    Thanks

  3. #3
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel for Office 365
    Posts
    1,702

    Re: Creating new sheets based off template

    This works brilliantly - its so quick! Thank you!

    Just a couple of questions..

    What does the turn extras on and off do?

    If I wanted to change the location of the list on the summary sheet from column C to D would it just be this part I need to change?
    lLastRow = shSummary.Cells(shSummary.Rows.Count, "C").End(xlUp).Row
    AND this maybe?
    ActiveSheet.Range("B2").Value = shSummary.Cells(i, 3).Value
    Paul

  4. #4
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Creating new sheets based off template

    Quote Originally Posted by pauldaddyadams View Post
    If I wanted to change the location of the list on the summary sheet from column C to D would it just be this part I need to change?
    lLastRow = shSummary.Cells(shSummary.Rows.Count, "C").End(xlUp).Row
    AND this maybe?
    ActiveSheet.Range("B2").Value = shSummary.Cells(i, 3).Value
    Yes you will have to change those that in the First case it says explicitly "C" and in the second one is says its number equivalent 3.

    Turn off extras and back on is a helper sub to turn off certain features that you don't need to have in Excel so the code runs faster.
    You don't need the screen to display new information until the whole thing is done.
    You don't need to calculate anything in the sheets until the whole thing is done...and so on.

    Thanks

  5. #5
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel for Office 365
    Posts
    1,702

    Re: Creating new sheets based off template

    Hi,

    I have tested this and I have decided to keep it in that column for ease.

    Still curious to know what the "extras" do.

    I do have one additional requirement - which is rather than just copy the template to and ensure the template has enough rows (based off the summary sheet) I will pop this up in a new post

+ 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. Creating a macro to reformat multiple sheets into a new template
    By Etachias in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-14-2013, 12:38 AM
  2. Taking Info from one work sheet and creating another based on a template
    By mcevil in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-08-2013, 09:13 AM
  3. [SOLVED] Creating Sheets from a Template
    By CRW in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-13-2012, 11:59 AM
  4. [SOLVED] Creating new sheets with Template
    By DanielRay in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 11-30-2011, 07:12 PM
  5. Creating dynamically sheets with data and template
    By Fulla in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-20-2011, 02:40 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