Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 90219

Excel VBA On Error Goto not working to stop sheet being added

$
0
0

My code is as below:

Sub NewWorksheetTest()
    Dim wsname As String
    wsname = InputBox("Enter a name for the new worksheet")

    On Error GoTo BadEntry
        Sheets.Add
        ActiveSheet.Name = wsname
        Exit Sub

BadEntry:
    MsgBox Err.Number & " :"& Err.Description, vbInformation, "There is an error...."

End Sub

My understanding is when i input something bad(e.g.duplicated names or names containing ?/), there is a message explaining the reasons and at the same time system stops a new sheet from being added. However when I try, the error msg is there but a new sheet keeps on being added to the workbook. Can anyone solve this please?:)


Viewing all articles
Browse latest Browse all 90219

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>