Add worksheet duplicate name error

zooka

New Member
Joined
May 4, 2022
Messages
2
Office Version
  1. 2021
Platform
  1. Windows
Hello! I am using the below code to insert new worksheets based on a list. The problem is, whenever I add a new client to the list, and run the code, I get error 1004 That name is already taken. I would like to get some help in order the code to skip the sheets already made and only add the new values.

Sub CreateSheetsFromList()
Dim ws As Worksheet, Ct As Long, c As Range
Set ws = Worksheets("minta")
Application.ScreenUpdating = False
For Each c In Sheets("kliensek").Range("A2:A50")
If c.Value <> "" Then
ws.Copy after:=Sheets(Sheets.Count)
ActiveSheet.Name = c.Value
Ct = Ct + 1
End If
Next c
If Ct > 0 Then
MsgBox Ct & " új kliens hozzáadva"
Else
MsgBox "Klienslista üres!"
End If
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hello! I am using the below code to insert new worksheets based on a list. The problem is, whenever I add a new client to the list, and run the code, I get error 1004 That name is already taken. I would like to get some help in order the code to skip the sheets already made and only add the new values.

Sub CreateSheetsFromList()
Dim ws As Worksheet, Ct As Long, c As Range
Set ws = Worksheets("minta")
Application.ScreenUpdating = False
For Each c In Sheets("kliensek").Range("A2:A50")
If c.Value <> "" Then
ws.Copy after:=Sheets(Sheets.Count)
ActiveSheet.Name = c.Value
Ct = Ct + 1
End If
Next c
If Ct > 0 Then
MsgBox Ct & " új kliens hozzáadva"
Else
MsgBox "Klienslista üres!"
End If
Application.ScreenUpdating = True
End Sub
Found the answer on the forum. Thank you!
 
Upvote 0
Did it look anything like this:
VBA Code:
Sub CreateSheetsFromList()
    Dim ws As Worksheet, Ct As Long, c As Range
    Dim wsTestExist As Worksheet
    Set ws = Worksheets("minta")
    Application.ScreenUpdating = False
    For Each c In Sheets("kliensek").Range("A2:A50")
        If c.Value <> "" Then
            On Error Resume Next
                Set wsTestExist = Worksheets(c.Value)
                If Err <> 0 Then
                    ws.Copy after:=Sheets(Sheets.Count)
                    ActiveSheet.Name = c.Value
                    Ct = Ct + 1
                End If
            On Error GoTo 0
            Set wsTestExist = Nothing
        End If
    Next c
    If Ct > 0 Then
        MsgBox Ct & " új kliens hozzáadva"
    Else
        MsgBox "Klienslista üres!"
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Something similar
VBA Code:
Sub CreateSheetsFromList()

    Dim ws As Worksheet, Ct As Long, c As Range
    Set ws = Worksheets("minta")
    Application.ScreenUpdating = False
    For Each c In Sheets("kliensek").Range("A2:A5")
        If c.Value <> "" Then
            Dim vWS As Worksheet
            For Each vWS In ActiveWorkbook.Worksheets
                If vWS.Name = c.Value Then GoTo EX:
            Next vWS
            ws.Copy after:=Sheets(Sheets.Count)
            ActiveSheet.Name = c.Value
            Ct = Ct + 1
EX:  End If
    Next c
    If Ct > 0 Then
        MsgBox Ct & " új kliens hozzáadva"
    Else
        MsgBox "Klienslista üres!"
    End If
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Thanks for posting that. I was trying to avoid looping through all the sheets on each pass. If you have a large number of sheets it may slow the macro down.
PS: Now that you have posted the actual code you are going to use, you can mark your own post that shows the code as a solution.
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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