Try this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Dim cell As Range
Dim sWks As String
Set r = Intersect(Target, Columns("A"))
If r Is Nothing Then Exit Sub
For Each cell In r
sWks = cell.Text
If SheetExists(sWks) Then
MsgBox "Sheet already exists: " & sWks, vbOKOnly
ElseIf Not IsValidSheetName(sWks) Then
MsgBox "Invalid sheet name: " & sWks
Else
Worksheets("Template").Copy After:=Worksheets(Worksheets.Count)
With Worksheets(Worksheets.Count)
.Name = sWks
.Range("A1") = sWks
.Visible = xlSheetVisible
End With
End If
Next cell
Me.Select
End Sub
Function SheetExists(sWks As String, _
Optional wkb As Workbook = Nothing) As Boolean
On Error Resume Next
SheetExists = Not IIf(wkb Is Nothing, ActiveWorkbook, wkb).Sheets(sWks) Is Nothing
If Err.Number Then Err.Clear
End Function
Function IsValidSheetName(s As String) As Boolean
If Len(s) = 0 Or Len(s) > 31 Then Exit Function
If InStr(s, "\") Then Exit Function
If InStr(s, "/") Then Exit Function
If InStr(s, ":") Then Exit Function
If InStr(s, "|") Then Exit Function
If InStr(s, "*") Then Exit Function
If InStr(s, "?") Then Exit Function
IsValidSheetName = True
End Function
There's a bug in Excel that if you try to copy the same worksheet too many times, it falls over, and you have to save, close, and reopen the workbook. This code does not circumvent that, but there are easy ways to.
Bookmarks