Seeking method to add sheets based on list and skip duplicates

auto.pilot

Well-known Member
Joined
Sep 27, 2007
Messages
734
Office Version
  1. 365
Platform
  1. 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?

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
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
How about
Code:
Sub CreateSheetsFromList()
   Application.ScreenUpdating = False
   Dim LastRow As Long
   Dim MyCell As Range
   Dim MyName As String
   
   Sheets("List").Select
   
   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.Value
         If Not Evaluate("isref('" & MyName & "'!A1)") Then
            Sheets("Template").Copy After:=Sheets(Sheets.count)
            ActiveSheet.Name = MyName
            Range("A1").Value = MyName
         End If
      End If
   Next MyCell
   Sheets("List").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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