If the sheet "Data" does not exist, the code works perfectly, if it does exist however I get the error "Name already exists, try a different name". I've simply fixed this with an On Error GoTo ErrorHandler
, the problem however is that after the code runs and the macro triggers the errorhandler msgbox , it still creates a new sheet regardless (with the names 'Sheet1,2,3,...').
Snippet:
Sub AddWorkSheet()
Dim wb As Workbook, shtDest As Worksheet
On Error GoTo ErrorHandler
Sheets.Add.Name = ("Data")
Set shtDest = Sheets("Data")
ErrorHandler:
MsgBox ("Something went wrong."), vbCritical
End Sub
I'd like the code to simply return the message box and not create any additional sheets if the sheet "Data" already exists.