+ Reply to Thread
Results 1 to 9 of 9

VBA to choose template, fill it in and then save workbook - impossible??????

Hybrid View

  1. #1
    Registered User
    Join Date
    03-03-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    39

    VBA to choose template, fill it in and then save workbook - impossible??????

    Hi all,
    I have spent around a month trying to find an appropriate solution to the task below but it’s been to no avail.

    Background:
    I have a workbook called ‘Data Sheet’ which contains a list of employee names (120+), which shift pattern they work, their managers names and individual stats based on their performance in a given quarter. I have spent a lot of time working on this so that these stats are pulled from various different sources rather than manually keyed – Progress!

    At the end of the quarter every manager needs to create a workbook with a 2 sheet performance review for each of their employees. (i.e. 100 employees with 10 managers would result in 10 workbooks being created with 20 sheets in each). This process is currently manual and very time consuming.

    To complicate things further, depending on what shift the employee works (D,R or DP) one of three different templates are needed.


    Task:
    I would like to have some VBA that does the following:
    • Look to see how many employees in the ‘Data Sheet’ are managed by a particular manager.
    • Of those employees, look to see if their shift is “D”, “R” or “DP”.
    • Create a new workbook and copy the correct 2 page template (based on their shift) into it for each employee managed by that manager.
    • Rename the first page of each template to the employee name.
    • Rename the second name of each template to the employee name + “Disc”.
    • Fill in each template using the relevant employees data contained in the ‘Data Sheet’ workbook.
    • Save the work book as the manager name.
    • Do the same for the next manager and so on.


    I know this is a big task but I would really appreciate someone’s genius on this.

    I have tried a lot of different coding and it’s now got to a point where I’m out of ideas and need to start again.

    Thank you.

    ps: I'm unable to upload any examples at the moment.

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: VBA to choose template, fill it in and then save workbook - impossible??????

    Hello and welcome to the forum. All the tasks you have noted are possible to code with loops, but that's not the problem that I see. If you are fortunate then one of the members will have some time to code all of this (and test it). If this thread goes cold, then I would advise you to start new threads with small bite sized problem and increment until you finish your project. And of course, there is little change of help with a dummy workbook.

    Good luck.

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

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

    Re: VBA to choose template, fill it in and then save workbook - impossible??????

    Post up your sample source workbook with the templates in it (sanitize the data to generic content, leave the layout alone), then post a sample "results" workbook for one manager and one or two of his him employees. We'll take a look from there.

    Click GO ADVANCED and use the paperclip icon to post up a copy of your workbook.
    _________________
    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!)

  4. #4
    Registered User
    Join Date
    03-03-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: VBA to choose template, fill it in and then save workbook - impossible??????

    Hi abousetta and JBeaucaire,
    Thanks for getting back to me so quick.

    I have attached a simpler version of my data sheet, templates and results as a lot of data is being pulled.
    However, I have not amended the formatting as requested.

    Thank you so much for your help.
    Attached Files Attached Files
    Last edited by dm@stams; 03-03-2012 at 03:51 PM. Reason: Spelling mistakes

  5. #5
    Registered User
    Join Date
    03-03-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: VBA to choose template, fill it in and then save workbook - impossible??????

    Hi JBeaucaire,
    I have just been looking at your 'Data into template' code and it looks very close to what I am trying to achieve.
    Would I be able to incorporate this along with additional if statements?
    Or am I going down the wrong route?

    Thanks.

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

    Re: VBA to choose template, fill it in and then save workbook - impossible??????

    That is there for the specific purpose you've found... to serve as a "beginning" for putting a row-based dataset into a standard template. So I would encourage you to try and adapt that very macro... yes, adding more "action" code to do more than the original sample. Build on it.

  7. #7
    Registered User
    Join Date
    03-03-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: VBA to choose template, fill it in and then save workbook - impossible??????

    I had seen and attempted to modify your coding previously but was overwhelmed by the task I am trying to complete. Thanks to your very clear coding I was able to create a template for every employee within the 'data sheet' very quickly.

    However, I would appreciate some advice as I cannot decide the best way to choose a template? Do I write code to complete the tasks listed in my original post (in order) or is there a simplier way. I am very much a novice when it comes to VBA and I know you guys are consistently hounded with requests/queries but if you could point me in the right direction that would be a great help.

  8. #8
    Registered User
    Join Date
    03-03-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: VBA to choose template, fill it in and then save workbook - impossible??????

    I have now managed to adapt coding to select the correct template.
    I decided to autofilter the 'data sheet' for each manager so that when I finish the project, I can just loop through each autofilter sub and then run the 'fill in template'.

    How can I adapt the code below to only take action if the row it's working on is not hidden via the autofilter?


    Sub FillOutTemplate()
    'Jerry Beaucaire  4/25/2010
    'From Sheet1 data fill out template on sheet2 and save
    'each sheet as its own file.
    Dim LastRw As Long, Rw As Long, Cnt As Long
    Dim dSht As Worksheet, rtSht1 As Worksheet, rtSht2 As Worksheet, dtSht1 As Worksheet, dtSht2 As Worksheet, dptSht1 As Worksheet, dptSht2 As Worksheet
    Dim MakeBooks As Boolean, SavePath As String
    Dim rng As Range, cell As Range
    Dim row As Range
    
    Application.ScreenUpdating = False  'speed up macro execution
    Application.DisplayAlerts = False   'no alerts, default answers used
    
    Set dSht = Sheets("Data Sheet")           'sheet with data on it starting in row2
    Set rtSht1 = Sheets("R Template sheet 1")       'sheet to copy and fill out
    Set rtSht2 = Sheets("R Template sheet 2")
    Set dtSht1 = Sheets("D Template sheet 1")
    Set dtSht2 = Sheets("D Template sheet 2")
    Set dptSht1 = Sheets("DP Template sheet 1")
    Set dptSht2 = Sheets("DP Template sheet 2")
    
    'Option to create separate workbooks
        MakeBooks = MsgBox("Create separate workbooks?" & vbLf & vbLf & _
            "YES = template will be copied to separate workbooks." & vbLf & _
            "NO = template will be copied to sheets within this same workbook", _
                vbYesNo + vbQuestion) = vbYes
    
    If MakeBooks Then   'select a folder for the new workbooks
        MsgBox "Please select a destination for the new workbooks"
        Do
            With Application.FileDialog(msoFileDialogFolderPicker)
                .AllowMultiSelect = False
                .Show
                If .SelectedItems.Count > 0 Then    'a folder was chosen
                    SavePath = .SelectedItems(1) & "\"
                    Exit Do
                Else                                'a folder was not chosen
                    If MsgBox("Do you wish to abort?", _
                        vbYesNo + vbQuestion) = vbYes Then Exit Sub
                End If
            End With
        Loop
    End If
    
    
        LastRw = dSht.Range("A" & Rows.Count).End(xlUp).row
            
        For Rw = 2 To LastRw
            If dSht.Range("D" & Rw).Value = ("R") Then
            rtSht1.Copy After:=Worksheets(Worksheets.Count)   'copy the template
            With ActiveSheet                                'fill out the form
                'edit these rows to fill out your form, add more as needed
                .Name = dSht.Range("B" & Rw)
                .Range("B3").Value = dSht.Range("A" & Rw).Value
                .Range("C4").Value = dSht.Range("B" & Rw).Value
                .Range("D5:D7").Value = dSht.Range("C" & Rw, "E" & Rw).Value
                rtSht2.Copy After:=Worksheets(Worksheets.Count)
                With ActiveSheet
                .Name = dSht.Range("B" & Rw) + " Disc"
            End With
            End With
    
                    
           ElseIf dSht.Range("D" & Rw).Value = "DP" Then
                dptSht1.Copy After:=Worksheets(Worksheets.Count)   'copy the template
                With ActiveSheet                                'fill out the form
                    'edit these rows to fill out your form, add more as needed
                    .Name = dSht.Range("B" & Rw)
                    .Range("B3").Value = dSht.Range("A" & Rw).Value
                    .Range("C4").Value = dSht.Range("B" & Rw).Value
                    .Range("D5:D7").Value = dSht.Range("C" & Rw, "E" & Rw).Value
                    dptSht2.Copy After:=Worksheets(Worksheets.Count)
                    With ActiveSheet
                    .Name = dSht.Range("B" & Rw) + " Disc"
                End With
                End With
    
            
            Else: dSht.Range("D" & Rw).Value = "D"
                dtSht1.Copy After:=Worksheets(Worksheets.Count)   'copy the template
                With ActiveSheet                                'fill out the form
                    'edit these rows to fill out your form, add more as needed
                    .Name = dSht.Range("B" & Rw)
                    .Range("B3").Value = dSht.Range("A" & Rw).Value
                    .Range("C4").Value = dSht.Range("B" & Rw).Value
                    .Range("D5:D7").Value = dSht.Range("C" & Rw, "E" & Rw).Value
                     dtSht2.Copy After:=Worksheets(Worksheets.Count)
                    With ActiveSheet
                    .Name = dSht.Range("B" & Rw) + " Disc"
            End With
            End With
    
            End If
    
                  
            
            If MakeBooks Then       'if making separate workbooks from filled out form
                ActiveSheet.Move
                ActiveWorkbook.SaveAs SavePath & Range("B3").Value, xlNormal
                ActiveWorkbook.Close False
            End If
            
            Cnt = Cnt + 1
    
    Next Rw
    
        dSht.Activate
        
        If MakeBooks Then
            MsgBox "Workbooks created: " & Cnt
        Else
            MsgBox "Worksheets created: " & Cnt
        End If
        
    Application.ScreenUpdating = True
    End Sub

    Many thanks.
    Last edited by dm@stams; 03-04-2012 at 11:35 AM. Reason: Code wrap

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

    Re: VBA to choose template, fill it in and then save workbook - impossible??????

    If you have applied a filter, then you can set a RNG variable for the remaining visible row. Then you can cycle through the "cells" in the RNG and use the cell.row:

    Dim LastRw As Long, RNG as Range, cell As Range
    
        LastRw = dSht.Range("A" & Rows.Count).End(xlUp).row
        Set RNG = dSht.Range("A2:A" & LR).SpecialCells(xlCellTypeVisible)
    
        For Each cell in RNG
            If dSht.Range("D" & cell.Row).Value = ("R") Then
                rtSht1.Copy After:=Worksheets(Worksheets.Count)     'copy the template
    
    ...etc

+ 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