I have a function that will add a worksheet and rename it, but first checks to see if a worksheet by that name already exists. To do that, I’m using the pretty widely available function here –
Function WorksheetExists(ByVal WorksheetName As String) As Boolean
On Error Resume Next ' Set to Resume Next as don't want to end macro if this fails
WorksheetExists = (Sheets(WorksheetName).Name <> "")
On Error GoTo 0
End Function
My problem is that I have already set On Error GoTo error_exit in the parent sub that calls this function, so On Error GoTo 0 after checking if the sheet exists nulifies that call.
I’ve tried On Error GoTo error_exit in this function, but I get the following error –
Complie Error: Label not defined
Does anybody know how I could work around this?
When you exit from the Function
WorksheetExistsyour error scope is automatically resumed to the previous situation.You don’t need to do anything to resume the
On Error Goto error_exitin the calling sub/function.It is interesting to note, that if you remove the error handling from this function and a worksheet with the passed name doesn’t exists, your code will resume execution on the calling error handling label (i.e. at the error_exit: label in the calling code)