Hi Bad boy,
Thanks for the rep!
- I used a temporary book and an array and I didn't code for the autofilter - so, if you manually filter your data:
Sub UBooks(): Dim wd As Workbook, wt As Workbook, wb As Workbook, ws As Worksheet, AF
Dim CN As String, CC As Long, DT As String, i As Long, j As Long: i = 2
Dim CPC As String, NPC As String, LOC As String, ASW() As String, n As Integer
Set wd = Workbooks("Data.xlsx"): Set wt = Workbooks("Template.xlsx")
Workbooks.Add: Set wb = ActiveWorkbook: Set ws = wb.Sheets("Sheet1")
wd.Worksheets("Sheet1").UsedRange.SpecialCells(xlCellTypeVisible).Copy ws.Range("A1")
AF = ws.UsedRange.Resize(ws.Range("A" & Rows.Count).End(xlUp).Row + 1, 10)
wb.Close False: Set wb = Nothing
GetUser: n = 1
CN = AF(i, 2): CC = AF(i, 6): DT = AF(i, 4)
If CN = "" Then Exit Sub
CPC = AF(i, 1): LOC = AF(i, 7) & " " & AF(i, 8)
Do Until AF(i + n, 2) <> CN: n = n + 1: Loop: ReDim ASW(n)
For j = 1 To n: ASW(j) = AF(i + j - 1, 10): Next j
wt.Worksheets("Sheet1").Copy: Set wb = ActiveWorkbook
With wb.Worksheets("Sheet1"): .Cells(2, 2) = CN: .Cells(2, 4) = CC: .Cells(2, 6) = DT
.Cells(3, 2) = CPC: .Cells(3, 6) = LOC: For j = 1 To n: .Cells(j + 9, 1) = ASW(j): Next j
End With
wb.SaveAs FileName:=wt.Path & "\" & CN
i = i + n: GoTo GetUser
End Sub
Bookmarks