excelvbatemplatespasting

Source data not being copied to template


I am running a macro (that I found online) that loops through data in Column C (C2:C100) in my source sheet, named "Data". For each value found in column C (unique values), a new worksheet is being created. The copying and pasting of data is spot on. I am trying to paste the data in another sheet called "Template" in Column C (C14:C100). This is where I am struggling. I'm not sure how and where to reference my Template. In addition to this, I would also like autonumber the newly pasted data on my destination sheet (Template) in column C, after the data has been copied. How do I do this?

Here is the code I've tried.

Sub MakeSheets()
Dim lr As Long
Dim ws As Worksheet
Dim i As Integer
Dim ar As Variant
Dim j As Long
Dim rng As Range

Application.ScreenUpdating = False

Set ws = Sheet3 'Sheets code name
lr = ws.Range("C" & Rows.Count).End(xlUp).Row
Set rng = ws.Range("C1:C" & lr)
j = ws.[C1].CurrentRegion.Columns.Count + 1

rng.AdvancedFilter 2, , ws.Cells(1, j), True
ar = ws.Range(ws.Cells(2, j), ws.Cells(Rows.Count, j).End(xlUp))
ws.Columns(j).Clear
    
For i = 1 To UBound(ar)
rng.AutoFilter 1, ar(i, 1)
If Not Evaluate("=ISREF('" & ar(i, 1) & "'!C1)") Then
Sheets.Add(After:=Sheets(Sheets.Count)).Name = ar(i, 1)
Else
Sheets(ar(i, 1)).Move After:=Sheets(Sheets.Count)
End If
ws.Range("C2:C" & lr).Resize(, j - 1).Copy [D14] 'Sheets("Template").Range("C14")
Next
ws.AutoFilterMode = False

Sheets("Template").Activate
Range("A1").Activate

Application.ScreenUpdating = True

End Sub

I would really like some guidance, as I am struggling with this. This is what the source sheet looks like:

Sheet "Data"

Data

My "Template" sheet

Template

Thanks in advance!


Solution

  • Here is the solution as per the latest comment:

    Sub MakeSheets()
      Dim lr As Long, i As Long, sz&, lc&, j&
      Dim ws As Worksheet, rng As Range, ar As Variant, rngtocopy As Range
      
      Application.ScreenUpdating = False
      
      Set ws = Sheet3
      lr = ws.[C1].End(xlDown).Row: lc = ws.[C1].End(xlToRight).Column
      Set rng = Range(ws.[C1], ws.Cells(lr, lc))
      
      ar = ws.Range(ws.[A2], ws.[A1].End(xlDown))
      
      For i = 1 To UBound(ar)
        rng.AutoFilter 1, ar(i, 1)
        Set rngtocopy = rng.Columns("B:" & Chr$(62 + lc)).Rows("2:" & lr).SpecialCells(xlCellTypeVisible)
        If Not Evaluate("=ISREF('" & ar(i, 1) & "'!C1)") Then
          Sheets("Template").Copy After:=Sheets(Sheets.Count)
          ActiveSheet.Name = ar(i, 1)
        Else
          Sheets(ar(i, 1)).Move After:=Sheets(Sheets.Count)
        End If
        With ActiveSheet
          .Rows("14:1000000").Clear
          rngtocopy.Copy .[D14]
          sz = .[D1000000].End(xlUp).Row - 13
          For j = 1 To sz
            .Cells(j + 13, 3) = j
          Next
        End With
      Next
      ws.AutoFilterMode = False
      
      Sheets("Template").Activate
      Range("A1").Activate
      
      Application.ScreenUpdating = True
    
    End Sub
    

    enter image description here