This got a bit ott so I gave up on it, but it provided different options for dealing with the name error, so I'll post it anyway.
Public Enum SheetExistsOption
overwrite
useexisting
cancelupdate
changename_auto
changename_prompt
End Enum
Sub AddSheet()
On Error Resume Next
Dim newS As Worksheet
Set newS = NewSheet(NewWS_Name, ActiveWorkbook, SheetExistsOption.changename_prompt)
If newS Is Nothing Then
MsgBox "There was an error creating the new sheet :("
Else
With newS
'hard coded ranges!
AddData .Range("A1"), data1
AddData .Range("A2"), data2
'display msg if this 'special' one errors
If Not AddData(.Range("A3"), data3) Then _
MsgBox "Error adding data" & Err.Description, _
vbExclamation, "Error " & Err.Number
End With
End If
End Sub
Public Function NewSheet(ShtName As String, wb As Workbook, seo As SheetExistsOption) As Worksheet
On Error Resume Next
If Not NameIsUnique(ShtName, wb) Then
Select Case seo
Case SheetExistsOption.cancelupdate
Exit Function
Case SheetExistsOption.changename_prompt
Dim rv As String
Do
ShtName = Trim(InputBox("Sheet name:", "Enter new sheet name", ShtName))
If ShtName = "" Then Exit Function
Loop Until NameIsUnique(ShtName, wb)
Case SheetExistsOption.changename_auto
Dim i As Integer, tmps As String
Do
i = i + 1
tmps = ShtName & "_" & i
If NameIsUnique(tmps, wb) Then
ShtName = tmps
Exit Do
End If
Loop
Case SheetExistsOption.overwrite
Set NewSheet = wb.Sheets(ShtName)
NewSheet.Cells.Delete
Exit Function
Case SheetExistsOption.useexisting
Set NewSheet = wb.Sheets(ShtName)
Exit Function
End Select
End If
Set NewSheet = CreateNamedSheet(ShtName, wb)
End Function
Public Function CreateNamedSheet(ShtName As String, wb As Workbook) As Worksheet
On Error Resume Next
Set CreateNamedSheet = wb.Sheets.Add(after:=Sheets(Sheets.Count), Type:=xlWorksheet)
If Err.Number = 0 Then
With CreateNamedSheet
.Name = ShtName
If Err.Number Then
CreateNamedSheet = Nothing
Application.DisplayAlerts = 0
.Delete
Application.DisplayAlerts = 1
End If
End With
End If
End Function
Public Function NameIsUnique(ShtName As String, wb As Workbook) As Boolean
On Error Resume Next
Dim newS As Worksheet
Set newS = wb.Worksheets(ShtName)
NameIsUnique = newS Is Nothing
End Function
Private Function AddData(rng As Range, val As Variant) As Boolean
On Error Resume Next
rng = val
If Err.Number Then
Debug.Print "error setting", rng.Address, "to",
Debug.Print val
Debug.Print
Else
AddData = True
End If
End Function