mr_excel_noob
New Member
- Joined
- May 1, 2019
- Messages
- 3
Using multiple questions & answers in this board, I've put together the following code to:
Now I need to add code to delete any sheets who's name isn't in the list [Range B11:B on "Master" Sheet] (i.e. if a row gets deleted), without also deleting sheets named "Master", "Template" and "Sheet2" as they are not in the list
Sorry I've probably explained poorly. Can anyone help?
- create new sheets based on a list (range B11:B in "Master" sheet),
- each sheet copied from a Template and renamed from the list,
- skip names with a sheet already made,
- delete sheets named "*Template (*" as they are created for some reason, and
- Return to "Master" sheet
Now I need to add code to delete any sheets who's name isn't in the list [Range B11:B on "Master" Sheet] (i.e. if a row gets deleted), without also deleting sheets named "Master", "Template" and "Sheet2" as they are not in the list
Sorry I've probably explained poorly. Can anyone help?
Code:
Sub CreateAndNameWorksheets()
Dim c As Range
Application.ScreenUpdating = False
For Each c In Sheets("Master").Range("B11:B" & Sheets("Master").Range("B" & Rows.Count).End(xlUp).Row)
Sheets("Template").Copy after:=Sheets(Sheets.Count)
With c
On Error Resume Next
ActiveSheet.Name = .Value
ActiveSheet.Protect
On Error GoTo 0
End With
Next c
Application.ScreenUpdating = True
Application.DisplayAlerts = False
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If ws.Name Like "*Template (*" Then ws.Delete
Next ws
Application.DisplayAlerts = True
Application.GoTo Worksheets("Master").Range("A1")
End Sub