I am currently working on a project, that consists generating various document examples in a new workbook, basing on the table data and document template. Here's what I have tried:
Sub CopyData()
Sheets("Staff").Select
For i = 2 To 100000
If Cells(i, 1).Value = "" Then
i = 100000
Exit For
End If
Sheets("TEMPLATE_TARGET").Select
Range("Name").Value = Sheets("Staff").Cells(i, 1).Value & " " & _
Range("Personalcode").Value = Sheets("Staff").Cells(i, 3).Value
Range("Residence").Value = Sheets("Staff").Cells(i, 4).Value
Range("Job").Value = Sheets("Staff").Cells(i, 5).Value
Cells.copy
Selection.Copy
Worksheets.Add.Name = Sheets("Staff").Cells(i, 1).Value
Application.DisplayAlerts = False
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("Staff").Select
Next i
MsgBox ("YAY")
End Sub
Unfortunately, my code's executing stops in the middle of process, claiming that the worksheet("Staff") is already existing and therefore - is out of range. How do you recommend to indicate the option to generate the final results as the new workbook to fix this issue? Thanks in advance.