auto.pilot
Well-known Member
- Joined
- Sep 27, 2007
- Messages
- 734
- Office Version
- 365
- Platform
- Windows
I have the following bit of code that simply creates sheets from a list in column D on my sheet called 'List'.
This all works perfectly with one exception. When the list includes a new sheet name that already exists, the code creates sheets called 'Duplicate 1', 'Duplicate 2', etc.
Instead, I would like to skip duplicates. IE: If a sheet on the list already exists, I'd like to skip that one and move to the next one.
How can I do that?
This all works perfectly with one exception. When the list includes a new sheet name that already exists, the code creates sheets called 'Duplicate 1', 'Duplicate 2', etc.
Instead, I would like to skip duplicates. IE: If a sheet on the list already exists, I'd like to skip that one and move to the next one.
How can I do that?
Code:
Sub CreateSheetsFromList()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim MyCell As Range
sheets("List").Select
c = 0
i = 0
LastRow = Cells(Rows.Count, 4).End(xlUp).Row
' sheet names in column D
For Each MyCell In Range("D1:D" & LastRow)
If Not MyCell = "" Then
MyName = MyCell
Skip:
On Error Resume Next
Set ws = Nothing
Set ws = ThisWorkbook.Worksheets(MyName)
On Error GoTo 0
If ws Is Nothing Then
sheets("Template").Copy After:=sheets(sheets.Count)
ActiveSheet.Name = MyName
Else
i = i + 1
sheets("Template").Copy After:=sheets(sheets.Count)
On Error Resume Next
ActiveSheet.Name = "Duplicate " & i
On Error GoTo 0
End If
ActiveSheet.Range("A1") = ActiveSheet.Name
End If
Next MyCell
sheets("List").Select
End Sub