Parse Functions‎ > ‎

Data into Template

"I have a worksheet database with each row representing an order.  I need to fill out a template with the data from each row and save the template as a separate form for each set of data."

  1. Data in standard row format starting at row2 on the data sheet
  2. Template will be copied to either a sheet in the same workbook or a workbook of it's own
  3. If separate workbooks are created, macro will ask for a destination folder for the new workbooks
  4. Macro will keep a count of workbooks/worksheets created for a double check
  5. Portions of code needing attention are highlighted
SAMPLE WORKBOOK:    FillOutTemplate-DuplicationMacro.xls


Option Explicit

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, tSht As Worksheet
Dim MakeBooks As Boolean, SavePath As String

Application.ScreenUpdating = False  'speed up macro execution
Application.DisplayAlerts = False   'no alerts, default answers used

Set dSht = Sheets("Data")           'sheet with data on it starting in row2
Set tSht = Sheets("Template")       'sheet to copy and fill out

'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"
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            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
End If

'Determine last row of data then loop through the rows one at a time
    LastRw = dSht.Range("A" & Rows.Count).End(xlUp).Row
    For Rw = 2 To LastRw
        tSht.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("A" & 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
        End With
        If MakeBooks Then       'if making separate workbooks from filled out form
            ActiveWorkbook.SaveAs SavePath & Range("B3").Value, xlNormal
            ActiveWorkbook.Close False
        End If
        Cnt = Cnt + 1
    Next Rw

    If MakeBooks Then
        MsgBox "Workbooks created: " & Cnt
        MsgBox "Worksheets created: " & Cnt
    End If
Application.ScreenUpdating = True
End Sub

Nothing says "thanks" like a steak dinner!
PayPal - The safer, easier way to pay online!