title: Sheet name already exists, how to handle the error and delete the default sheet in Excel?
body: <p>Creating a sheet with a name from sheet1 works great. However, if I mistakenly run the code again, it gives runtime 1004 error, because the newly created sheet name already exists. This is fine, but how can I handle this error and prevent Excel from creating one default sheet with a random name? </p>
  
  <p>I want Excel to MsgBox me that there is a duplicate and it should then do nothing. I know there are similar threads, but they didn't answer my question. Thanks. </p>
  
  <pre><code>Private Sub CreateSheet()
      Dim ws As Worksheet
      Set ws = ThisWorkbook.Sheets.Add(After:= _
               Worksheets("Sheet1"))
  
      Application.DisplayAlerts = False
  
      ws.Name = Sheets("Sheet1").Range("N1")
  
      On Error Resume Next
  
      If (Worksheets("ws.Name").Name &lt;&gt; "") Then
  
      MsgBox ("name already exists, please delete")
  
      Else
  
      ws.Visible = xlSheetVisible
  
      ThisWorkbook.Sheets("Sheet1").Range("A:AB").Copy
      ThisWorkbook.Sheets(ws.Name).Range("A1").PasteSpecial Paste:=xlPasteValues
  
      End If
  
  End Sub
  </code></pre>