Hello Excel Whizzes, another usual call for help.
my vba code below looks at my names list in a range e.g A:A, takes each value, copies a master template worksheet and renames it to the first value in the list range, hides the template and then moves on to the next name on the list till they're done.
Question 1. I have about 200 names in the range but excel of crashing when processing these and giving a memory error. is is because I am not clearing the variable Newpagename for each loop iteration? or is there something wrong with my code.
Question 2. I also struggling to find out why I end up with may other template sheet copies like template (2) - (15) etc.
Question 3. How do I amend my code below to first check if a sheet with the same name exists and if so to skip addition process the addition and move to the next?
Many many Thanks in advance!
my vba code below looks at my names list in a range e.g A:A, takes each value, copies a master template worksheet and renames it to the first value in the list range, hides the template and then moves on to the next name on the list till they're done.
Question 1. I have about 200 names in the range but excel of crashing when processing these and giving a memory error. is is because I am not clearing the variable Newpagename for each loop iteration? or is there something wrong with my code.
Question 2. I also struggling to find out why I end up with may other template sheet copies like template (2) - (15) etc.
Question 3. How do I amend my code below to first check if a sheet with the same name exists and if so to skip addition process the addition and move to the next?
Code:
Sub Initialise_CmrPages()
Dim i As Long
Dim ws As Worksheet
Dim Newpagename As String
Set ws = ActiveSheet
With ws
For i = 2 To .Range("A" & Rows.Count).End(3).Row
On Error Resume Next
'Make the Template sheet visible, and copy it
With Worksheets("template")
.Visible = xlSheetVisible
.Activate
End With
'Copy template sheet
Sheets("template").Copy After:=Worksheets(Worksheets.Count)
Newpagename = .Cells(i, "A").Value
ActiveWindow.ActiveSheet.Name = Newpagename
Sheets("template").Visible = False
'Hide Template sheet
Worksheets("template").Visible = xlSheetVeryHidden
Next i
End With
End Sub
Many many Thanks in advance!