The code below copy's the "Template" and renames based on a list. It works perfectly fine until there is a duplicate name.
How can a message box be used to indicate that an error occurred, and skip the sheet that contained the duplicate name?
I'm fairly new to VBA, an explanation on how to resolve this error would be very helpful.
Here's the code I'm using....
How can a message box be used to indicate that an error occurred, and skip the sheet that contained the duplicate name?
I'm fairly new to VBA, an explanation on how to resolve this error would be very helpful.
Here's the code I'm using....
VBA Code:
Option Explicit
Sub NewSheets()
Dim xRg As Excel.Range
Dim i As Integer
Dim ws As Worksheet
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim response As String
Set ws = Sheets("Template")
Set sh1 = Sheets("Sheet_Names")
Set sh2 = Sheets("Data_Sheet")
Application.ScreenUpdating = 0
For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
Sheets("Template").Copy before:=sh2
ActiveSheet.Name = sh1.Range("A" & i).Value
If Err.Number = 1004 Then 'name already used
response = MsgBox("This sheet will be skipped as the name arelady exists", vbYesNo)
If response = vbNo Then GoTo NextIteration
End If
NextIteration:
Next i
End Sub