vba macro to batch process addition of multiple worksheets

SKooLZ

New Member
Joined
Oct 7, 2014
Messages
30
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?

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!

 

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top