I have a sub which should only continue running if 4 specific worksheets are present. From online reading, such as @Tim Williams's comment in Test or check if sheet exists, I know that using an error thrown, to determine a course of action, can be frowned upon, but I found similar usage to mine in both that question and here.
Here is a simplified version:
Private Sub Test() If AllSheetsPresent Then MsgBox "Doing something" End Sub Private Function AllSheetsPresent() As Boolean AllSheetsPresent = True Dim SheetsArr() On Error GoTo errhand SheetsArr = ThisWorkbook.Sheets(Array("Response Times", "Incidents", "Calls", "Resources", "NoC, CPR")) Exit Function errhand: If Err.Number = 9 Then AllSheetsPresent = False MsgBox "Sheets are missing from..." End End If End Function
For the purposes of testing I am using
ThisWorkbook. It would otherwise be a workbook variable passed to the function.
Is there a better way to perform this validation?
Related to 1) are there any risks associated with this approach I should be aware of?
If AllSheetsPresent Then MsgBox "Doing something"feels in some way superfluous as if False the code has stopped anyway. Again, this feels like an extension of 1) can this be re-written in a better way?
As this is working code I have posted here in the hope it is the right place to ask.