Consulting

Results 1 to 3 of 3

Thread: create workbooks based on a given template using data in a table

  1. #1

    Exclamation create workbooks based on a given template using data in a table

    an example of data I have on sheet two
    Name bio Chem phy lug
    John 40 60 50 20
    Luke 70 50 30 77
    Eric 80 90 89 60
    I want to create a workbook per name using a template on sheet one . The name should be in c11, bio score in D17, phy score in d 18, Chem score in d19 ..., each workbook name should correspond to the name in c 11. and the workbooks saved in c :\ reports \ .

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Try this


    Option Explicit
    Sub MakeStudentBooks()
        Dim wbTeacher As Workbook, wbStudent As Workbook
        Dim wsTemplate As Worksheet, wsGrades As Worksheet
        Dim rGrades As Range
        Dim iStudent As Long
        Dim sStudentWorkbook As String
        
        'set variables
        Set wbTeacher = ThisWorkbook
        Set wsTemplate = Worksheets("Sheet1")
        Set wsGrades = Worksheets("Sheet2")     '   use better names
        Set rGrades = wsGrades.Cells(1, 1).CurrentRegion
        
        
        Application.ScreenUpdating = False
        
        
        'go down student list, skip header in row 1
        For iStudent = 2 To rGrades.Rows.Count
            
            wsTemplate.Range("C11").Value = rGrades.Cells(iStudent, 1).Value
            wsTemplate.Range("D17").Value = rGrades.Cells(iStudent, 2).Value
            wsTemplate.Range("D18").Value = rGrades.Cells(iStudent, 3).Value
            wsTemplate.Range("D19").Value = rGrades.Cells(iStudent, 4).Value
            wsTemplate.Range("D20").Value = rGrades.Cells(iStudent, 5).Value
            
            'copy template to new workbook
            wsTemplate.Copy
        
            'remember the new workbook which is now active
            Set wbStudent = ActiveWorkbook
            
    '        sStudentWorkbook = "C:\Reports\" & rGrades.Cells(iStudent, 1).Value & ".xlsx"
            sStudentWorkbook = "C:\Users\Daddy\Documents\Reports\" & rGrades.Cells(iStudent, 1).Value & ".xlsx"
            
            'delete it if it exists, continue if it doesn't
            On Error Resume Next
            Kill sStudentWorkbook
            On Error GoTo 0
            
            'save with student name
            wbStudent.SaveAs Filename:=sStudentWorkbook, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            ActiveWindow.Close
        
            wbTeacher.Activate
        
        Next iStudent
        
        wsTemplate.Range("C11").ClearContents
        wsTemplate.Range("D17").ClearContents
        wsTemplate.Range("D18").ClearContents
        wsTemplate.Range("D19").ClearContents
        wsTemplate.Range("D20").ClearContents
        Application.ScreenUpdating = True
     
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    works great. thnx so much

Posting Permissions

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